diff --git a/enum.c b/enum.c index 609438bfdb..56cef1726d 100644 --- a/enum.c +++ b/enum.c @@ -2527,10 +2527,15 @@ enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj) { VALUE n, size; long slice_size = NUM2LONG(RARRAY_AREF(args, 0)); + ID infinite_p; + CONST_ID(infinite_p, "infinite?"); if (slice_size <= 0) rb_raise(rb_eArgError, "invalid slice size"); size = enum_size(obj, 0, 0); if (size == Qnil) return Qnil; + if (RB_FLOAT_TYPE_P(size) && RTEST(rb_funcall(size, infinite_p, 0))) { + return size; + } n = add_int(size, slice_size-1); return div_int(n, slice_size); diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index 6bc776dee5..54dfebb814 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -113,6 +113,11 @@ class TestEnumerator < Test::Unit::TestCase assert_equal([[1,2,3],[4,5,6],[7,8,9],[10]], (1..10).each_slice(3).to_a) end + def test_each_slice_size + assert_equal(4, (1..10).each_slice(3).size) + assert_equal(Float::INFINITY, 1.step.each_slice(3).size) + end + def test_cons a = [[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [7,8,9], [8,9,10]] assert_equal(a, (1..10).each_cons(3).to_a)