Ruby | Array class fetch() operation
Array#fetch() : fetch() is a Array class method which returns the element at the argument index position.
Syntax: Array.fetch()
Parameter: index value
Return: element at the argument index value
Code #1 : Example for fetch() method
a = [ 18 , 22 , 33 , nil , 5 , 6 ]
b = [ 1 , 4 , 1 , 1 , 88 , 9 ]
c = [ 18 , 22 , nil , nil , 50 , 6 ]
puts "fetch : #{a.fetch(5)}\n\n"
puts "fetch : #{b.fetch(0)}\n\n"
puts "fetch : #{c.fetch(2)}\n\n"
|
Output :
fetch : 6
fetch : 1
fetch :
Code #2 : Example for fetch() method
a = [ "abc" , "nil" , "dog" ]
b = [ "cow" , nil , "dog" ]
c = [ "cat" , nil , nil ]
puts "fetch : #{a.fetch(2)}\n\n"
puts "fetch : #{b.fetch(0)}\n\n"
puts "fetch : #{c.fetch(1)}\n\n"
|
Output :
fetch : dog
fetch : cow
fetch :
Last Updated :
08 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...