mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
Make Range#reverse_each raise TypeError if endless
This commit is contained in:
parent
0164da68c1
commit
1a16b6ffc2
2 changed files with 13 additions and 0 deletions
5
range.c
5
range.c
|
@ -1133,6 +1133,11 @@ range_reverse_each(VALUE range)
|
||||||
VALUE end = RANGE_END(range);
|
VALUE end = RANGE_END(range);
|
||||||
int excl = EXCL(range);
|
int excl = EXCL(range);
|
||||||
|
|
||||||
|
if (NIL_P(end)) {
|
||||||
|
rb_raise(rb_eTypeError, "can't iterate from %s",
|
||||||
|
rb_obj_classname(end));
|
||||||
|
}
|
||||||
|
|
||||||
if (FIXNUM_P(beg) && FIXNUM_P(end)) {
|
if (FIXNUM_P(beg) && FIXNUM_P(end)) {
|
||||||
if (excl) {
|
if (excl) {
|
||||||
if (end == LONG2FIX(FIXNUM_MIN)) return range;
|
if (end == LONG2FIX(FIXNUM_MIN)) return range;
|
||||||
|
|
|
@ -582,6 +582,14 @@ class TestRange < Test::Unit::TestCase
|
||||||
assert_equal([fmin-2, fmin-3], a)
|
assert_equal([fmin-2, fmin-3], a)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_reverse_each_for_endless_range
|
||||||
|
assert_raise(TypeError) { (1..).reverse_each {} }
|
||||||
|
|
||||||
|
enum = nil
|
||||||
|
assert_nothing_raised { enum = (1..).reverse_each }
|
||||||
|
assert_raise(TypeError) { enum.each {} }
|
||||||
|
end
|
||||||
|
|
||||||
def test_reverse_each_for_single_point_range
|
def test_reverse_each_for_single_point_range
|
||||||
fmin = RbConfig::LIMITS['FIXNUM_MIN']
|
fmin = RbConfig::LIMITS['FIXNUM_MIN']
|
||||||
fmax = RbConfig::LIMITS['FIXNUM_MAX']
|
fmax = RbConfig::LIMITS['FIXNUM_MAX']
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue