Ruby | Enumerable find_all() function

The find_all() of enumerable is an inbuilt method in Ruby returns the items in the enumerable which satisfies the given condition in the block. It returns an enumerator if no block is given.

Syntax: enu.find_all { |obj| block }

Parameters: The function takes a block whose condition is used to find the elements.



Return Value: It returns the items in the enum which satisfies the condition in the block. Iy t returns an enumerator if no block is given.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for find_all method in Enumerable
  
# Initialize 
enu = (1..10)
  
# Prints
enu.find_all { |obj| obj % 2 == 1}

chevron_right


Output:

[1, 3, 5, 7, 9]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for find_all method in Enumerable
  
# Initialize 
enu = [1, 7, 10, 11]
  
# Prints
enu.find_all

chevron_right


Output:

Enumerator: [1, 7, 10, 11]:find_all


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.