Open In App
Related Articles

Ruby | Enumerable find() function

Improve Article
Improve
Save Article
Save
Like Article
Like

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:




# Ruby program for find method in Enumerable
  
# Initialize
enu = (1..50)
  
# returns first element 
enu.find { |el|  el % 2 == 0 && el % 9 == 0}


Output:

18

Example 2:




# Ruby program for find method in Enumerable
  
# Initialize
enu = (1..50)
  
# returns enumerator
enu.find


Output:

Enumerator: 1..50:find
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 05 Dec, 2019
Like Article
Save Article
Previous
Next
Similar Reads