Ruby | at() function
Last Updated :
23 Feb, 2023
The at() function in Ruby is used to return the element of the specified index. Index 0 is for the first element of the array, 1 for the second element of the array and so on. The negative index counts from the end of the input array.
Syntax: Array.at(Index) Here Array is the input array of elements at which at() function is called. Parameters: Index : Its corresponding elements are to be returned and this index may be negative, positive or zero. Returns: the corresponding elements whose index is taken as the parameter.
Example 1:
Ruby
Array = ["a", "b", "c", "d", "e", "gfg",
"Geeks", "Geek", "GeeksforGeeks"]
A = Array .at( 0 )
B = Array .at( 1 )
C = Array .at( 3 )
D = Array .at( 5 )
E = Array .at(- 1 )
F = Array .at(- 3 )
puts "
puts "
puts "
puts "
puts "
puts "
|
Output:
a
b
d
gfg
GeeksforGeeks
Geeks
Example 2:
Ruby
Array = [ 0 , 1 , 2 , 3 , 4 , 10 , 20 , 30 , 40 ]
puts "
puts "
puts "
puts "
puts "
puts "
|
Output:
0
1
3
10
40
20
Reference: https://devdocs.io/ruby~2.5/array#method-i-at
Share your thoughts in the comments
Please Login to comment...