Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Enumerable#min_by

enum.min_by {|obj| block } → obj
enum.min_by → an_enumerator
enum.min_by(n) {|obj| block } → array
enum.min_by(n) → an_enumerator

Returns the object in enum that gives the minimum value from the given block.

If no block is given, an enumerator is returned instead.

Example
a = %w(albatross dog horse)
a.min_by { |x| x.length }   #=> "dog"

If the n argument is given, minimum n elements are returned as an array. These n elements are sorted by the value from the given block.

Example
a = %w[albatross dog horse]
p a.min_by(2) {|x| x.length } #=> ["dog", "horse"]
See also #min #minmax #member? #minmax_by also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1