ruby-changes:53088
From: knu <ko1@a...>
Date: Mon, 22 Oct 2018 20:24:01 +0900 (JST)
Subject: [ruby-changes:53088] knu:r65302 (trunk): Set the size of a new enumerator created by Enumerator#each with arguments to nil
knu 2018-10-22 20:23:56 +0900 (Mon, 22 Oct 2018) New Revision: 65302 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65302 Log: Set the size of a new enumerator created by Enumerator#each with arguments to nil When each() takes arguments, it is never safe to assume that the iteration would repeat the same number of times as with each() without any argument. Actually, there is no way to get the exact number, so the size should be set to nil to denote that. Modified files: trunk/enumerator.c trunk/test/ruby/test_enumerator.rb Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 65301) +++ test/ruby/test_enumerator.rb (revision 65302) @@ -301,8 +301,11 @@ class TestEnumerator < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enumerator.rb#L301 yield end ary = [] - e = o.to_enum.each(ary) - e.next + e = o.to_enum { 1 } + assert_equal(1, e.size) + e_arg = e.each(ary) + assert_equal(nil, e_arg.size) + e_arg.next assert_equal([1], ary) end Index: enumerator.c =================================================================== --- enumerator.c (revision 65301) +++ enumerator.c (revision 65302) @@ -550,6 +550,8 @@ enumerator_each(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/enumerator.c#L550 args = rb_ary_new4(argc, argv); } e->args = args; + e->size = Qnil; + e->size_fn = 0; } if (!rb_block_given_p()) return obj; return enumerator_block_call(obj, 0, obj); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/