Home »
Ruby Tutorial
Ruby at() function
By IncludeHelp Last updated : December 01, 2024
If you are working with arrays in Ruby, sometimes you may need to find the element at a particular index. For meeting the purpose, we have got at() function in Ruby which is already defined in Ruby's library.
at() function in Ruby
The basic purpose of at() function is to return the element stored at the specified index which is passed as an argument. The index may be positive, negative or zero. If we invoke at() function with 0 index, it will return the first element of the array, likewise, if we invoke at() function with 1 index, it will return the second element of the array. In case of negative indexes, at() with Index -1 will return the last element of the array, at() with Index -2 will return the second last element of the array, at() with Index -3 will return the third last element of the array and so on subject to availability.
Syntax
Array_name.at(Index)
Now, let us understand the implementation of at() function with the help of provided examples.
Example 1
=begin
Ruby program to demonstrate implementation of at() function
=end
# Initialising array of elements
Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"]
# Calling to at() function
Var1 = Arr.at(0)
Var2 = Arr.at(1)
Var3 = Arr.at(3)
Var4 = Arr.at(5)
Var5 = Arr.at(-1)
Var6 = Arr.at(-3)
# Getting the corresponding elements
# whose indexes are given as parameter
puts "#{Var1}"
puts "#{Var2}"
puts "#{Var3}"
puts "#{Var4}"
puts "#{Var5}"
puts "#{Var6}"
Output
C++
C
Python
Visual Basic
Includehelp
C#
Explanation
In the above code, we have initialized an array named 'arr'. We are invoking at() with different indexes as an argument. arr.at(0) is returning the first element of the array and coming towards negative indexes, at(-1) is returning the last element of the array.
Example 2
=begin
Ruby program to demonstrate implementation of at() function
=end
# Initialising array of elements
Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"]
#Taking index from user
puts "Enter the index of the element you want to search"
i = gets.chomp.to_i
puts "The element at index #{i} is #{Arr.at(i)}"
Output
Run 1:
Enter the index of the element you want to search
3
The element at index 3 is Python
Run 2:
Enter the index of the element you want to search
-1
The element at index -1 is Includehelp
Explanation
You can observe in the above code that we are taking an index as an input from the user and invoking at() function with that index. The function is returning values corresponding to that index.
Example 3
=begin
Ruby program to demonstrate implementation of at() function
=end
# Initialising array of elements
Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"]
#Storing value in the variable
p=Arr.at(1)
q=Arr.at(3)
puts p
puts q
Output
C
Python
Explanation
We are invoking at() function inside variables. The variable is storing the value which is returned from the at() method. The value can be used for further processing.