Method: Range#cover?
- Defined in:
- range.c
permalink #cover?(obj) ⇒ Boolean #cover?(range) ⇒ Boolean
Returns true
if obj
is between the begin and end of the range.
This tests begin <= obj <= end
when #exclude_end? is false
and begin <= obj < end
when #exclude_end? is true
.
If called with a Range argument, returns true
when the given range is covered by the receiver, by comparing the begin and end values. If the argument can be treated as a sequence, this method treats it that way. In the specific case of (a..b).cover?(c...d)
with a <= c && b < d
, the end of the sequence must be calculated, which may exhibit poor performance if c
is non-numeric. Returns false
if the begin value of the range is larger than the end value. Also returns false
if one of the internal calls to <=>
returns nil
(indicating the objects are not comparable).
("a".."z").cover?("c") #=> true
("a".."z").cover?("5") #=> false
("a".."z").cover?("cc") #=> true
("a".."z").cover?(1) #=> false
(1..5).cover?(2..3) #=> true
(1..5).cover?(0..6) #=> false
(1..5).cover?(1...6) #=> true
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 |
# File 'range.c', line 1613
static VALUE
range_cover(VALUE range, VALUE val)
{
VALUE beg, end;
beg = RANGE_BEG(range);
end = RANGE_END(range);
if (rb_obj_is_kind_of(val, rb_cRange)) {
return RBOOL(r_cover_range_p(range, beg, end, val));
}
return r_cover_p(range, beg, end, val);
}
|