Ruby | Enumerable find() function
Last Updated :
05 Dec, 2019
The find() of enumerable is an inbuilt method in Ruby returns the first element which satisfies the given condition in the block. If there is no block, then it returns the enumerator itself.
Syntax: block.find { |obj| block }
Parameters: The function takes the block according to which the first which satisfies is to be returned.
Return Value: It returns the first element which satisfies the block or the enumerator instead.
Example 1:
enu = ( 1 .. 50 )
enu.find { |el| el % 2 == 0 && el % 9 == 0 }
|
Output:
18
Example 2:
Output:
Enumerator: 1..50:find
Share your thoughts in the comments
Please Login to comment...