End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Array#minmax
array.minmax → [min_val, max_val]
array.minmax {|a, b| ... } → [min_val, max_val]
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 new 2-element Array containing the minimum and maximum values from self, either per method <=> or per a given block:.
When no block is given, each element in self must respond to method <=> with an Integer; returns a new 2-element Array containing the minimum and maximum values from self, per method <=>:
Example
[0, 1, 2].minmax # => [0, 2]
When a block is given, the block must return an Integer; the block is called self.size-1 times to compare elements; returns a new 2-element Array containing the minimum and maximum values from self, per the block:
Example
['0', '00', '000'].minmax {|a, b| a.size <=> b.size } # => ["0", "000"]