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)
{
NODE *memo = NEW_MEMO(Qundef, Qundef, Qundef);
struct minmax_t *m = (struct minmax_t *)&memo->u1.value;
VALUE ary = rb_ary_new3(2, Qnil, Qnil);
m->min = Qundef;
m->last = Qundef;
if (rb_block_given_p()) {
rb_block_call(obj, id_each, 0, 0, minmax_ii, (VALUE)memo);
if (m->last != Qundef)
minmax_ii_update(m->last, m->last, m);
}
else {
rb_block_call(obj, id_each, 0, 0, minmax_i, (VALUE)memo);
if (m->last != Qundef)
minmax_i_update(m->last, m->last, m);
}
if (m->min != Qundef) {
rb_ary_store(ary, 0, m->min);
rb_ary_store(ary, 1, m->max);
}
return ary;
}