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
array.c
View on GitHub ↗
static VALUE
rb_ary_minmax(VALUE ary)
{
if (rb_block_given_p()) {
return rb_call_super(0, NULL);
}
return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
}
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.