Ruby | Enumerable find_index() function
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:
enu = [ 8 , 9 , 10 , 14 ]
enu.find_index { |obj| obj % 2 == 1 }
|
Output:
1
Example 2:
enu = ( 1 .. 6 )
puts enu.find_index( 4 )
puts enu.find_index( 7 )
|
Output:
3
nil
Last Updated :
05 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...