[前][次][番号順一覧][スレッド一覧]

ruby-changes:65903

From: Yusuke <ko1@a...>
Date: Wed, 21 Apr 2021 13:02:50 +0900 (JST)
Subject: [ruby-changes:65903] fb04c69418 (master): array.c (rb_ary_zip): take only as many as needed from an Enumerator (#4389)

https://git.ruby-lang.org/ruby.git/commit/?id=fb04c69418

From fb04c69418ceee696a114fe31279cf3a5ea16d30 Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Wed, 21 Apr 2021 13:02:29 +0900
Subject: array.c (rb_ary_zip): take only as many as needed from an Enumerator
 (#4389)

[Bug #17814]
---
 array.c                 |  4 ++--
 test/ruby/test_array.rb | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/array.c b/array.c
index f97be64..76f1158 100644
--- a/array.c
+++ b/array.c
@@ -4311,10 +4311,9 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4311
 take_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, cbarg))
 {
     VALUE *args = (VALUE *)cbarg;
-    if (args[1] == 0) rb_iter_break();
-    else args[1]--;
     if (argc > 1) val = rb_ary_new4(argc, argv);
     rb_ary_push(args[0], val);
+    if (--args[1] == 0) rb_iter_break();
     return Qnil;
 }
 
@@ -4324,6 +4323,7 @@ take_items(VALUE obj, long n) https://github.com/ruby/ruby/blob/trunk/array.c#L4323
     VALUE result = rb_check_array_type(obj);
     VALUE args[2];
 
+    if (n == 0) return result;
     if (!NIL_P(result)) return rb_ary_subseq(result, 0, n);
     result = rb_ary_new2(n);
     args[0] = result; args[1] = (VALUE)n;
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index bf6a728..4a9863c 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -2735,6 +2735,17 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2735
     assert_equal [[42, 1]], [42].zip(r), bug8153
   end
 
+  def test_zip_with_enumerator
+    bug17814 = "ruby-core:103513"
+
+    step = 0.step
+    e = Enumerator.produce { step.next }
+    a = %w(a b c)
+    assert_equal([["a", 0], ["b", 1], ["c", 2]], a.zip(e))
+    assert_equal([["a", 3], ["b", 4], ["c", 5]], a.zip(e))
+    assert_equal([["a", 6], ["b", 7], ["c", 8]], a.zip(e))
+  end
+
   def test_transpose
     assert_equal([[1, :a], [2, :b], [3, :c]],
       [[1, 2, 3], [:a, :b, :c]].transpose)
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]