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

ruby-changes:69771

From: Jeremy <ko1@a...>
Date: Wed, 17 Nov 2021 12:31:46 +0900 (JST)
Subject: [ruby-changes:69771] a5cff7cc5d (master): Make Enumerable#each_cons return object if over size

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

From a5cff7cc5de374be05db8e99c4ca975e60558a99 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Tue, 16 Nov 2021 13:43:00 -0800
Subject: Make Enumerable#each_cons return object if over size

This behavior changed in dfb47bbd17c3c2b8ce17dbafaf62df023b0224b2,
but only for normal exit, not for early exit.  Fix it for early
exit as well.

While here, fix example code in documentation so that it doesn't
indicate that the method returns nil.
---
 enum.c                 | 10 +++++-----
 test/ruby/test_enum.rb |  2 ++
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/enum.c b/enum.c
index a51b90296b7..f8e327ff7f5 100644
--- a/enum.c
+++ b/enum.c
@@ -2968,12 +2968,12 @@ enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj) https://github.com/ruby/ruby/blob/trunk/enum.c#L2968
  *  returns +self+:
  *
  *    a = []
- *    (1..10).each_slice(3) {|tuple| a.push(tuple) } # => nil
+ *    (1..10).each_slice(3) {|tuple| a.push(tuple) }
  *    a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
  *
  *    a = []
  *    h = {foo: 0, bar: 1, baz: 2, bat: 3, bam: 4}
- *    h.each_slice(2) {|tuple| a.push(tuple) } # => nil
+ *    h.each_slice(2) {|tuple| a.push(tuple) }
  *    a # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]
  *
  *  With no block given, returns an Enumerator.
@@ -3047,12 +3047,12 @@ enum_each_cons_size(VALUE obj, VALUE args, VALUE eobj) https://github.com/ruby/ruby/blob/trunk/enum.c#L3047
  *  returns +self+:
  *
  *    a = []
- *    (1..5).each_cons(3) {|element| a.push(element) } # => nil
+ *    (1..5).each_cons(3) {|element| a.push(element) }
  *    a # => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
  *
  *    a = []
  *    h = {foo: 0,  bar: 1, baz: 2, bam: 3}
- *    h.each_cons(2) {|element| a.push(element) } # => nil
+ *    h.each_cons(2) {|element| a.push(element) }
  *    a # => [[[:foo, 0], [:bar, 1]], [[:bar, 1], [:baz, 2]], [[:baz, 2], [:bam, 3]]]
  *
  *  With no block given, returns an Enumerator.
@@ -3068,7 +3068,7 @@ enum_each_cons(VALUE obj, VALUE n) https://github.com/ruby/ruby/blob/trunk/enum.c#L3068
     if (size <= 0) rb_raise(rb_eArgError, "invalid size");
     RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_cons_size);
     arity = rb_block_arity();
-    if (enum_size_over_p(obj, size)) return Qnil;
+    if (enum_size_over_p(obj, size)) return obj;
     memo = MEMO_NEW(rb_ary_new2(size), dont_recycle_block_arg(arity), size);
     rb_block_call(obj, id_each, 0, 0, each_cons_i, (VALUE)memo);
 
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index b71ed546532..f6375a4ffc2 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -739,6 +739,7 @@ class TestEnumerable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L739
     assert_equal([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], ary)
 
     assert_equal(1..10, (1..10).each_slice(3) { })
+    assert_equal([], [].each_slice(3) { })
   end
 
   def test_each_cons
@@ -760,6 +761,7 @@ class TestEnumerable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L761
     assert_empty(ary)
 
     assert_equal(1..5, (1..5).each_cons(3) { })
+    assert_equal([], [].each_cons(3) { })
   end
 
   def test_zip
-- 
cgit v1.2.1


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

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