Array#each_index() : each_index() is a Array class method which returns the index of the array element by following the condition in the given block once for each_index element in self.
Syntax: Array.each_index()
Parameter: block - condition to follow
Return: index of the array element following the condition
Code #1 : Example for each_index() method
a = [ "abc" , "nil" , "dog" ]
b = [ "hello" , "hi" , "dog" ]
puts "each_index : #{a.each_index {|x| print x, " -- "}}\n\n"
|
Output :
0 -- 1 -- 2 -- each_index : ["abc", "nil", "dog"]
Code #2 : Example for each_index() method
a = [ "abc" , "nil" , "dog" ]
b = [ "hello" , "hi" , "dog" ]
puts "each_index : #{b.each_index{|x| x = 2}}\n\n"
|
Output :
each_index : ["hello", "hi", "dog"]