Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Ruby | Array class each_index() operation

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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"


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"
     


Output :

each_index : ["hello", "hi", "dog"]
My Personal Notes arrow_drop_up
Last Updated : 08 Jan, 2020
Like Article
Save Article
Similar Reads