diff options
author | Kouhei Yanagita <[email protected]> | 2023-10-13 15:02:23 +0900 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2024-04-10 07:28:07 -0700 |
commit | 9f6deaa6888a423720b4b127b5314f0ad26cc2e6 (patch) | |
tree | 0545536f0c556825b6ffa5fdd1c884a33c48982e /range.c | |
parent | f9f25d0ed001ae7c7a335c32fb3c5f0895709b9b (diff) |
[Misc #18984] Raise TypeError from Range#size if the range is not iterable
Diffstat (limited to 'range.c')
-rw-r--r-- | range.c | 18 |
1 files changed, 12 insertions, 6 deletions
@@ -827,7 +827,12 @@ sym_each_i(VALUE v, VALUE arg) * (1..4).size # => 4 * (1...4).size # => 3 * (1..).size # => Infinity - * ('a'..'z').size #=> nil + * ('a'..'z').size # => nil + * + * If +self+ is not iterable, raises an exception: + * + * (0.5..2.5).size # TypeError + * (..1).size # TypeError * * Related: Range#count. */ @@ -836,7 +841,8 @@ static VALUE range_size(VALUE range) { VALUE b = RANGE_BEG(range), e = RANGE_END(range); - if (rb_obj_is_kind_of(b, rb_cNumeric)) { + + if (RB_INTEGER_TYPE_P(b)) { if (rb_obj_is_kind_of(e, rb_cNumeric)) { return ruby_num_interval_step_size(b, e, INT2FIX(1), EXCL(range)); } @@ -844,10 +850,10 @@ range_size(VALUE range) return DBL2NUM(HUGE_VAL); } } - else if (NIL_P(b)) { - if (rb_obj_is_kind_of(e, rb_cNumeric)) { - return DBL2NUM(HUGE_VAL); - } + + if (!discrete_object_p(b)) { + rb_raise(rb_eTypeError, "can't iterate from %s", + rb_obj_classname(b)); } return Qnil; |