Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Array#minmax

() -> [ Elem?, Elem? ]
() { (Elem a, Elem b) -> ::Comparable::_CompareToZero } -> [ Elem?, Elem? ]
() { (Elem a, Elem b) -> ::Comparable::_CompareToZero? } -> [ Elem?, Elem? ]
minmax → array
minmax {|a, b| ... } → array

Returns a 2-element array containing the minimum-valued and maximum-valued elements from self; does not modify self.

With no block given, the minimum and maximum values are determined using method <=>:

Example
[1, 0, 3, 2].minmax # => [0, 3]

With a block given, the block must return a numeric; the block is called self.size - 1 times to compare elements; returns the elements having the minimum and maximum values per the block:

Example
['0', '', '000', '00'].minmax {|a, b| a.size <=> b.size }
# => ["", "000"]

Related: see Methods for Fetching.

See also #min #none? #max #one? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0