End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Range#min
rng.min → obj
rng.min {| a,b | block } → obj
rng.min(n) → array
rng.min(n) {| a,b | block } → array
range.c
View on GitHub ↗
static VALUE
range_min(int argc, VALUE *argv, VALUE range)
{
if (NIL_P(RANGE_BEG(range))) {
rb_raise(rb_eRangeError, "cannot get the minimum of beginless range");
}
if (rb_block_given_p()) {
if (NIL_P(RANGE_END(range))) {
rb_raise(rb_eRangeError, "cannot get the minimum of endless range with custom comparison method");
}
return rb_call_super(argc, argv);
}
else if (argc != 0) {
return range_first(argc, argv, range);
}
else {
struct cmp_opt_data cmp_opt = { 0, 0 };
VALUE b = RANGE_BEG(range);
VALUE e = RANGE_END(range);
int c = NIL_P(e) ? -1 : OPTIMIZED_CMP(b, e, cmp_opt);
if (c > 0 || (c == 0 && EXCL(range)))
return Qnil;
return b;
}
}
Returns the minimum value in the range. Returns nil if the begin value of the range is larger than the end value. Returns nil if the begin value of an exclusive range is equal to the end value.
Can be given an optional block to override the default comparison method a <=> b.
Example
(10..20).min #=> 10