Ruby | Enumerable detect() function
The detect() 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.detect { |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 detect method in Enumerable # Initialize enu = (1..50) # returns first element enu.detect { |el| el % 2 == 0 && el % 9 == 0} |
Output:
18
Example 2:
# Ruby program for detect method in Enumerable # Initialize enu = (1..50) # returns enumerator enu.detect |
Output:
Enumerator: 1..50:detect
Recommended Posts:
- Ruby | Enumerable map() function
- Ruby | Enumerable max() function
- Ruby | Enumerable none?() function
- Ruby | Enumerable all? function
- Ruby | Enumerable min() function
- Ruby | Enumerable one? function
- Ruby | Enumerable any? function
- Ruby | Enumerable take() function
- Ruby | Enumerable first() function
- Ruby | Enumerable sum() function
- Ruby | Enumerable reject() function
- Ruby | Enumerable grep() function
- Ruby | Enumerable select() function
- Ruby | Enumerable to_a() function
- Ruby | Enumerable grep_v() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


