Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Enumerable find_index() function

  • Last Updated : 05 Dec, 2019

The find_index() of enumerable is an inbuilt method in Ruby returns the index of the item which returns true for the given condition in the block, or the index of the item which is equal to the given value. If no block is given, then it returns an enumerator. If the values is not present in the enumerable, then it returns nil.

Syntax: enu.find_index { |obj| block } or enu.find (val)

Parameters: The function takes a block whose condition is used to find the first element which is true or takes the value whose first occurrence is to be searched for.

Return Value: It returns the index.

Example 1:




# Ruby program for find_index method in Enumerable
  
# Initialize 
enu = [8, 9, 10, 14]
  
# Prints
enu.find_index { |obj| obj % 2 == 1}

Output:

1

Example 2:




# Ruby program for find_index method in Enumerable
  
# Initialize 
enu = (1..6)
  
# Prints
puts enu.find_index(4)
  
puts enu.find_index(7)

Output:

3
nil

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!