Home » Ruby programming

Accessing first and last element of an Array using Array.first and Array.last methods in Ruby

Here, we are going to learn how to access the first and the last element of an Array using Array.first and Array.last methods in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 11, 2019

In Ruby, you have the privilege to access the first and last method of an array directly without putting some different logic for it with the help of Array.first and Array.last methods.

In case you want to get the first letter of the Array you may use Array.at(0) method because indexing starts with 0 or it can be directly done with Array.first but if you have to find the last element of the Array, you will have to first find the length of the Array then you will have to subtract the length by 1 and then print it with the help of Array.at(Array.count()-1) method. The process gets a little hectic. So, for making the task simpler, we have Array.last method which will directly return you the last element of the Array with the lesser lines of code.

Now, let us see how both methods are implemented with their syntaxes and Ruby code.

Syntax:

    # first element
    Array.first

    # last element
    Array.last

The scenarios will be clearer to you when you will go through their examples. The first example belongs to Array.first and latter belongs to Array.last.

Example 1:

# array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# Getting first element using Array.at() method 
puts "Array.at(0) method"
puts "The first element of Array : #{Adc.at(0)}"

# Getting first element using Array.first method 
puts "Array.first method"
puts "The first element of Array : #{Adc.first}"

Output

Array.at(0) method
The first element of Array : Includehelp.com
Array.first method
The first element of Array : Includehelp.com

Explanation:

In the above code, you can understand that we are retrieving the first element of the Array with the help of two methods, firstly with the help of Array.at() method and then with the help of Array.first method. Both methods are giving the desired output.

Example 2:

# array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# Getting last element using Array.at() method 
puts "Array.at(Array.count()-1) method"
puts "The first element of Array : #{Adc.at(Adc.count()-1)}"

# Getting last element using Array.last method 
puts "Array.last method"
puts "The first element of Array : #{Adc.last}"

Output

Array.at(Array.count()-1) method
The first element of Array : Python
Array.last method
The first element of Array : Python

Explanation:

In the above code, you can observe that we are using two different methods for the completion of the same task. The first method is Array.at() along with Array.count(). We can achieve the same thing by avoiding the use of two different methods and adopting the Array.last method. Both ways are giving us the desired output. Array.last method is considered to be very effective for this purpose because it is defined to serve this purpose only.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.