aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--numeric.c14
-rw-r--r--test/ruby/test_enumerator.rb1
2 files changed, 14 insertions, 1 deletions
diff --git a/numeric.c b/numeric.c
index fbec4d1768..b36908a8aa 100644
--- a/numeric.c
+++ b/numeric.c
@@ -3473,6 +3473,18 @@ int_downto(VALUE from, VALUE to)
return from;
}
+static VALUE
+int_dotimes_size(VALUE num)
+{
+ if (FIXNUM_P(num)) {
+ if (NUM2LONG(num) <= 0) return INT2FIX(0);
+ }
+ else {
+ if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0);
+ }
+ return num;
+}
+
/*
* call-seq:
* int.times {|i| block } -> self
@@ -3495,7 +3507,7 @@ int_downto(VALUE from, VALUE to)
static VALUE
int_dotimes(VALUE num)
{
- RETURN_ENUMERATOR(num, 0, 0);
+ RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);
if (FIXNUM_P(num)) {
long i, end;
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index fe00aa7e57..2c5158e726 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -506,6 +506,7 @@ class TestEnumerator < Test::Unit::TestCase
def test_size_for_loops
assert_equal Float::INFINITY, loop.size
+ assert_equal 42, 42.times.size
end
def test_size_for_each_slice