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

enum.minmax → [min, max]
enum.minmax { |a, b| block } → [min, max]

Returns a two element array which contains the minimum and the maximum value in the enumerable. The first form assumes all objects implement <=>; the second uses the block to return a <=> b.

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