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:
a = [ "gopal" , "tunday" , "geeks" , "classes" , "linux" ]
p a.min_by( 2 ) {|obj| obj.length }
p a.min_by {|obj| obj.length }
|
Output:
["geeks", "gopal"]
"gopal"
Example #2:
a = [ "gopal" , "tunday" , "geeks" , "classes" , "linux" ]
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
Share your thoughts in the comments
Please Login to comment...