Range#max
static VALUE
range_max(int argc, VALUE *argv, VALUE range)
{
VALUE e = RANGE_END(range);
int nm = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric);
if (NIL_P(RANGE_END(range))) {
rb_raise(rb_eRangeError, "cannot get the maximum of endless range");
}
VALUE b = RANGE_BEG(range);
if (rb_block_given_p() || (EXCL(range) && !nm)) {
if (NIL_P(b)) {
rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
}
return rb_call_super(argc, argv);
}
else if (argc) {
VALUE ary[2];
ID reverse_each;
CONST_ID(reverse_each, "reverse_each");
rb_scan_args(argc, argv, "1", &ary[0]);
ary[1] = rb_ary_new2(NUM2LONG(ary[0]));
rb_block_call(range, reverse_each, 0, 0, first_i, (VALUE)ary);
return ary[1];
#if 0
if (integer_end_optimizable(range)) {
return rb_int_range_last(argc, argv, range, true);
}
return rb_ary_reverse(rb_ary_last(argc, argv, rb_Array(range)));
#endif
}
else {
int c = NIL_P(b) ? -1 : OPTIMIZED_CMP(b, e);
if (c > 0)
return Qnil;
if (EXCL(range)) {
if (!RB_INTEGER_TYPE_P(e)) {
rb_raise(rb_eTypeError, "cannot exclude non Integer end value");
}
if (c == 0) return Qnil;
if (!NIL_P(b) && !RB_INTEGER_TYPE_P(b)) {
rb_raise(rb_eTypeError, "cannot exclude end value with non Integer begin value");
}
if (FIXNUM_P(e)) {
return LONG2NUM(FIX2LONG(e) - 1);
}
return rb_int_minus(e,INT2FIX(1));
}
return e;
}
}
Returns the maximum value in self, using method <=> or a given block for comparison.
With no argument and no block given, returns the maximum-valued element of self.
(1..4).max # => 4 ('a'..'d').max # => "d" (-4..-1).max # => -1
With non-negative integer argument n given, and no block given, returns the n maximum-valued elements of self in an array:
(1..4).max(2) # => [4, 3] ('a'..'d').max(2) # => ["d", "c"] (-4..-1).max(2) # => [-1, -2] (1..4).max(50) # => [4, 3, 2, 1]
If a block is given, it is called:
-
First, with the first two element of
self. -
Then, sequentially, with the so-far maximum value and the next element of
self.
To illustrate:
(1..4).max {|a, b| p [a, b]; a <=> b } # => 4
Output:
[2, 1] [3, 2] [4, 3]
With no argument and a block given, returns the return value of the last call to the block:
(1..4).max {|a, b| -(a <=> b) } # => 1
With non-negative integer argument n given, and a block given, returns the return values of the last n calls to the block in an array:
(1..4).max(2) {|a, b| -(a <=> b) } # => [1, 2] (1..4).max(50) {|a, b| -(a <=> b) } # => [1, 2, 3, 4]
Returns an empty array if n is zero:
(1..4).max(0) # => [] (1..4).max(0) {|a, b| -(a <=> b) } # => []
Returns nil or an empty array if:
-
The begin value of the range is larger than the end value:
Example(4..1).max # => nil (4..1).max(2) # => [] (4..1).max {|a, b| -(a <=> b) } # => nil (4..1).max(2) {|a, b| -(a <=> b) } # => []
-
The begin value of an exclusive range is equal to the end value:
Example(1...1).max # => nil (1...1).max(2) # => [] (1...1).max {|a, b| -(a <=> b) } # => nil (1...1).max(2) {|a, b| -(a <=> b) } # => []
Raises an exception if either:
-
selfis a endless range:(1..). -
A block is given and
selfis a beginless range.
Related: Range#min, Range#minmax.
Ruby version changes
-
4.0
-
Range#maxbehavior on beginless integer ranges has been fixed. [Bug #21174] [Bug #21175]
-