minmax()
public
Returns two elements array which contains the minimum and the maximum value
in the enumerable. The first form
assumes all objects implement Comparable;
the second uses the block to return a <=> b.
a = %w(albatross dog horse)
a.minmax
a.minmax {|a,b| a.length <=> b.length }
Show source
static VALUE
enum_minmax(VALUE obj)
{
struct minmax_t memo;
VALUE ary = rb_ary_new3(2, Qnil, Qnil);
memo.min = Qundef;
memo.last = Qundef;
if (rb_block_given_p()) {
rb_block_call(obj, id_each, 0, 0, minmax_ii, (VALUE)&memo);
if (memo.last != Qundef)
minmax_ii_update(memo.last, memo.last, &memo);
}
else {
rb_block_call(obj, id_each, 0, 0, minmax_i, (VALUE)&memo);
if (memo.last != Qundef)
minmax_i_update(memo.last, memo.last, &memo);
}
if (memo.min != Qundef) {
rb_ary_store(ary, 0, memo.min);
rb_ary_store(ary, 1, memo.max);
}
return ary;
}