Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Enumerable min_by() function

  • Last Updated : 05 Dec, 2019

The min_by() of enumerable is an inbuilt method in Ruby returns an array of minimum elements which satisfies the condition of the given block. It returns an enumerator when no block is given.

Syntax: enu.min_by(n) { |obj| block }

Parameters: The function takes two parameters n and block. N signifies the number of minimum elements and block signifies the condition.

Return Value: It returns a min element or an array containing N min elements.

Example #1:






# Ruby program for min_by method in Enumerable
  
# Initialize 
a = ["gopal", "tunday", "geeks", "classes", "linux"]
  
# Prints
p a.min_by(2) {|obj| obj.length }
  
p a.min_by {|obj| obj.length }

Output:

["geeks", "gopal"]
"gopal"

Example #2:




# Ruby program for min_by method in Enumerable
  
# Initialize 
a = ["gopal", "tunday", "geeks", "classes", "linux"]
  
# Prints
p a.min_by(2
  
p a.min_by 

Output:

Enumerator: ["gopal", "tunday", "geeks", "classes", "linux"]:min_by(2)
Enumerator: ["gopal", "tunday", "geeks", "classes", "linux"]:min_by
My Personal Notes arrow_drop_up
Recommended Articles
Page :