Ruby | Array rindex() function
Array#rindex() : rindex() is a Array class method which returns the index of the last object in the array.
Syntax: Array.rindex()
Parameter: Array
Return: the index of the last object in the array.
if not present then nil
Example #1 :
a = [ 18 , 22 , 33 , nil , 5 , 6 ]
b = [ 1 , 4 , 1 , 1 , 88 , 9 ]
c = [ 18 , 22 , 50 , 6 ]
puts "rindex() method form : #{a.rindex(22)}\n\n"
puts "rindex() method form : #{b.rindex(1)}\n\n"
puts "rindex() method form : #{c.rindex(18)}\n\n"
|
Output :
rindex() method form : 1
rindex() method form : 3
rindex() method form : 0
Example #2 :
a = [ "abc" , "nil" , "dog" ]
c = [ "cat" , nil ]
b = [ "cow" , nil , "dog" ]
puts "rindex() method form : #{a.rindex(" abc ")}\n\n"
puts "rindex() method form : #{b.rindex(" cat ")}\n\n"
puts "rindex() method form : #{c.rindex(" cat ")}\n\n"
|
Output :
rindex() method form : 0
rindex() method form :
rindex() method form : 0
Last Updated :
06 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...