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#minmax_by

enum.minmax_by { |obj| block } → [min, max]
enum.minmax_by → an_enumerator

Returns a two element array containing the objects in enum that correspond to the minimum and maximum values respectively from the given block.

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

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