Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Array at() operation

  • Last Updated : 08 Jan, 2020

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




# 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"

Output :

element : 5

element : 6

element : 88

element : 33

Code #2 : Example for at() method




# 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"

Output :

element : 

element : dog

element : cat

element : dog



My Personal Notes arrow_drop_up
Recommended Articles
Page :