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
/*
* call-seq:
* enum.minmax => [min,max]
* enum.minmax {|a,b| block } => [min,max]
*
* Returns two elements array which contains the minimum and the
* maximum value in the enumerable. The first form assumes all
* objects implement <code>Comparable</code>; the second uses the
* block to return <em>a <=> b</em>.
*
* a = %w(albatross dog horse)
* a.minmax #=> ["albatross", "horse"]
* a.minmax {|a,b| a.length <=> b.length } #=> ["dog", "albatross"]
*/
static VALUE
enum_minmax(obj)
VALUE obj;
{
VALUE result[3];
VALUE ary = rb_ary_new3(2, Qnil, Qnil);
result[0] = Qundef;
if (rb_block_given_p()) {
result[2] = ary;
rb_block_call(obj, id_each, 0, 0, minmax_ii, (VALUE)result);
}
else {
rb_block_call(obj, id_each, 0, 0, minmax_i, (VALUE)result);
}
if (result[0] != Qundef) {
RARRAY(ary)->ptr[0] = result[0];
RARRAY(ary)->ptr[1] = result[1];
}
return ary;
}