diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-06-02 20:00:35 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-06-02 20:00:35 -0700 |
commit | 17af8bfce6508951edca8650155be5525a894f65 (patch) | |
tree | aa76ef5e567dca3b56937f2cbf18a43576a34397 | |
parent | f48aad7ba22811dcd5e5be7af667ec49301ad7cd (diff) |
Make size on an infinite each_slice enumerator return Infinity
Fixes [Bug #15889]
-rw-r--r-- | enum.c | 5 | ||||
-rw-r--r-- | test/ruby/test_enumerator.rb | 5 |
2 files changed, 10 insertions, 0 deletions
@@ -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) |