Ruby | Array class each_index() operation
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
# Ruby code for each_index() method # declaring array a = ["abc", "nil", "dog"] # declaring array b = ["hello", "hi", "dog"] # each_index puts "each_index : #{a.each_index {|x| print x, " -- "}}\n\n" |
chevron_right
filter_none
Output :
0 -- 1 -- 2 -- each_index : ["abc", "nil", "dog"]
Code #2 : Example for each_index() method
# Ruby code for each_index() method # declaring array a = ["abc", "nil", "dog"] # declaring array b = ["hello", "hi", "dog"] # each_index puts "each_index : #{b.each_index{|x| x = 2}}\n\n" |
chevron_right
filter_none
Output :
each_index : ["hello", "hi", "dog"]

