Method: Array#minmax

Defined in:
array.c

#minmaxArray #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 #<=>:

[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:

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

Related: see Methods for Fetching.

Overloads:

  • #minmaxArray

    Returns:

  • #minmax {|a, b| ... } ⇒ Array

    Yields:

    • (a, b)

    Returns:



6260
6261
6262
6263
6264
6265
6266
6267
# File 'array.c', line 6260

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));
}