diff options
-rw-r--r-- | enum.c | 14 | ||||
-rw-r--r-- | test/ruby/test_enumerator.rb | 9 | ||||
-rw-r--r-- | test/ruby/test_lazy_enumerator.rb | 9 | ||||
-rw-r--r-- | version.h | 6 |
4 files changed, 29 insertions, 9 deletions
@@ -2810,17 +2810,19 @@ cycle_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary)) static VALUE enum_cycle_size(VALUE self, VALUE args, VALUE eobj) { - long mul; + long mul = 0; VALUE n = Qnil; - VALUE size = enum_size(self, args, 0); - - if (size == Qnil) return Qnil; + VALUE size; if (args && (RARRAY_LEN(args) > 0)) { n = RARRAY_AREF(args, 0); + if (!NIL_P(n)) mul = NUM2LONG(n); } - if (n == Qnil) return DBL2NUM(INFINITY); - mul = NUM2LONG(n); + + size = enum_size(self, args, 0); + if (NIL_P(size) || FIXNUM_ZERO_P(size)) return size; + + if (NIL_P(n)) return DBL2NUM(INFINITY); if (mul <= 0) return INT2FIX(0); return rb_funcall(size, '*', 1, LONG2FIX(mul)); } diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index bd6b7fa669..51f69be37d 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -576,13 +576,22 @@ class TestEnumerator < Test::Unit::TestCase assert_equal Float::INFINITY, [:foo].cycle.size assert_equal 10, [:foo, :bar].cycle(5).size assert_equal 0, [:foo, :bar].cycle(-10).size + assert_equal Float::INFINITY, {foo: 1}.cycle.size + assert_equal 10, {foo: 1, bar: 2}.cycle(5).size + assert_equal 0, {foo: 1, bar: 2}.cycle(-10).size assert_equal 0, [].cycle.size assert_equal 0, [].cycle(5).size + assert_equal 0, {}.cycle.size + assert_equal 0, {}.cycle(5).size assert_equal nil, @obj.cycle.size assert_equal nil, @obj.cycle(5).size assert_equal Float::INFINITY, @sized.cycle.size assert_equal 126, @sized.cycle(3).size + assert_equal Float::INFINITY, [].to_enum { 42 }.cycle.size + assert_equal 0, [].to_enum { 0 }.cycle.size + + assert_raise(TypeError) {[].to_enum { 0 }.cycle("").size} end def test_size_for_loops diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index e6888583f9..56890f4b6e 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -501,6 +501,15 @@ EOS assert_equal Float::INFINITY, loop.lazy.cycle.size assert_equal nil, lazy.select{}.cycle(4).size assert_equal nil, lazy.select{}.cycle.size + + class << (obj = Object.new) + def each; end + def size; 0; end + include Enumerable + end + lazy = obj.lazy + assert_equal 0, lazy.cycle.size + assert_raise(TypeError) {lazy.cycle("").size} end def test_map_zip @@ -1,10 +1,10 @@ #define RUBY_VERSION "2.4.4" -#define RUBY_RELEASE_DATE "2018-03-10" -#define RUBY_PATCHLEVEL 258 +#define RUBY_RELEASE_DATE "2018-03-16" +#define RUBY_PATCHLEVEL 259 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 3 -#define RUBY_RELEASE_DAY 10 +#define RUBY_RELEASE_DAY 16 #include "ruby/version.h" |