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
# Ruby code for fetch() method # declaring array a = [ 18 , 22 , 33 , nil , 5 , 6 ] # declaring array b = [ 1 , 4 , 1 , 1 , 88 , 9 ] # declaring array c = [ 18 , 22 , nil , nil , 50 , 6 ] # fetch puts "fetch : #{a.fetch(5)}\n\n" # fetch puts "fetch : #{b.fetch(0)}\n\n" # fetch puts "fetch : #{c.fetch(2)}\n\n" |
Output :
fetch : 6 fetch : 1 fetch :
Code #2 : Example for fetch() method
# Ruby code for fetch() method # declaring array a = [ "abc" , "nil" , "dog" ] # declaring array b = [ "cow" , nil , "dog" ] # declaring array c = [ "cat" , nil , nil ] # fetch puts "fetch : #{a.fetch(2)}\n\n" # fetch puts "fetch : #{b.fetch(0)}\n\n" # fetch puts "fetch : #{c.fetch(1)}\n\n" |
Output :
fetch : dog fetch : cow fetch :
Please Login to comment...