Open In App

Ruby | Enumerable select() function

Improve
Improve
Like Article
Like
Save
Share
Report

The select() 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.select { |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:




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


Output:

[1, 3, 5, 7, 9]

Example 2:




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


Output:

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

Last Updated : 05 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads