Ruby | Array at() operation

Array#at() : at() is an Array class method which returns the element at the specific argumented index value.

Syntax:  Array.at()

Parameter: 
- Arrays to search elements. 
- index to search

Return:  Array element at a specific index value

Code #1 : Example for at() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for at() method
   
# declaring array
a = [18, 22, 33, 4, 5, 6]
   
# declaring array
b = [5, 4, 22, 1, 88, 9]
   
# declaring array
c = [18, 22, 33, 40, 50, 16]
  
# finding the array element at 
# given index
  
puts "element : #{a.at(4)}\n\n"
  
puts "element : #{a.at(-1)}\n\n"
  
puts "element : #{b.at(-2)}\n\n"
  
puts "element : #{c.at(-4)}\n\n"

chevron_right


Output :

element : 5

element : 6

element : 88

element : 33

Code #2 : Example for at() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for at() method
   
# declaring array
a = ["abc", "xyz", "dog"]
   
# declaring array
b = ["cow", "cat", "dog"]
   
# declaring array
c = ["cat", "1", "dog"]
  
# finding the array element at 
# given index
puts "element : #{a.at(4)}\n\n"
  
puts "element : #{a.at(-1)}\n\n"
  
puts "element : #{b.at(-2)}\n\n"
  
puts "element : #{c.at(2)}\n\n"

chevron_right


Output :

element : 

element : dog

element : cat

element : dog



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.