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

ruby-changes:69406

From: TSUYUSATO <ko1@a...>
Date: Mon, 25 Oct 2021 12:13:58 +0900 (JST)
Subject: [ruby-changes:69406] dfb47bbd17 (master): Fix `Enumerable#each_cons` and `Enumerable#each_slice` to return a receiver

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

From dfb47bbd17c3c2b8ce17dbafaf62df023b0224b2 Mon Sep 17 00:00:00 2001
From: TSUYUSATO Kitsune <make.just.on@g...>
Date: Mon, 25 Oct 2021 12:13:44 +0900
Subject: Fix `Enumerable#each_cons` and `Enumerable#each_slice` to return a
 receiver

Co-authored-by: Takashi Kokubun <takashikkbn@g...>
Co-authored-by: Nobuyoshi Nakada <nobu@r...>
---
 enum.c                                       | 12 ++++++------
 spec/ruby/core/enumerable/each_cons_spec.rb  |  4 ++--
 spec/ruby/core/enumerable/each_slice_spec.rb |  4 ++--
 test/ruby/test_enum.rb                       |  4 ++++
 4 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/enum.c b/enum.c
index b54acf957ce..d3384f523e9 100644
--- a/enum.c
+++ b/enum.c
@@ -2961,11 +2961,11 @@ enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj) https://github.com/ruby/ruby/blob/trunk/enum.c#L2961
 
 /*
  *  call-seq:
- *    each_slice(n) { ... }  ->  nil
+ *    each_slice(n) { ... }  ->  self
  *    each_slice(n)          ->  enumerator
  *
  *  Calls the block with each successive disjoint +n+-tuple of elements;
- *  returns +nil+:
+ *  returns +self+:
  *
  *    a = []
  *    (1..10).each_slice(3) {|tuple| a.push(tuple) } # => nil
@@ -2997,7 +2997,7 @@ enum_each_slice(VALUE obj, VALUE n) https://github.com/ruby/ruby/blob/trunk/enum.c#L2997
     ary = memo->v1;
     if (RARRAY_LEN(ary) > 0) rb_yield(ary);
 
-    return Qnil;
+    return obj;
 }
 
 static VALUE
@@ -3040,11 +3040,11 @@ enum_each_cons_size(VALUE obj, VALUE args, VALUE eobj) https://github.com/ruby/ruby/blob/trunk/enum.c#L3040
 
 /*
  *  call-seq:
- *    each_cons(n) { ... } ->  nil
+ *    each_cons(n) { ... } ->  self
  *    each_cons(n)         ->  enumerator
  *
  *  Calls the block with each successive overlapped +n+-tuple of elements;
- *  returns +nil+:
+ *  returns +self+:
  *
  *    a = []
  *    (1..5).each_cons(3) {|element| a.push(element) } # => nil
@@ -3072,7 +3072,7 @@ enum_each_cons(VALUE obj, VALUE n) https://github.com/ruby/ruby/blob/trunk/enum.c#L3072
     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);
 
-    return Qnil;
+    return obj;
 }
 
 static VALUE
diff --git a/spec/ruby/core/enumerable/each_cons_spec.rb b/spec/ruby/core/enumerable/each_cons_spec.rb
index 7d44f54f744..ba658203a26 100644
--- a/spec/ruby/core/enumerable/each_cons_spec.rb
+++ b/spec/ruby/core/enumerable/each_cons_spec.rb
@@ -10,7 +10,7 @@ describe "Enumerable#each_cons" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/enumerable/each_cons_spec.rb#L10
 
   it "passes element groups to the block" do
     acc = []
-    @enum.each_cons(3){|g| acc << g}.should be_nil
+    @enum.each_cons(3){|g| acc << g}
     acc.should == @in_threes
   end
 
@@ -27,7 +27,7 @@ describe "Enumerable#each_cons" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/enumerable/each_cons_spec.rb#L27
 
   it "tries to convert n to an Integer using #to_int" do
     acc = []
-    @enum.each_cons(3.3){|g| acc << g}.should == nil
+    @enum.each_cons(3.3){|g| acc << g}
     acc.should == @in_threes
 
     obj = mock('to_int')
diff --git a/spec/ruby/core/enumerable/each_slice_spec.rb b/spec/ruby/core/enumerable/each_slice_spec.rb
index ab3b79c3448..2ea89f5e72e 100644
--- a/spec/ruby/core/enumerable/each_slice_spec.rb
+++ b/spec/ruby/core/enumerable/each_slice_spec.rb
@@ -10,7 +10,7 @@ describe "Enumerable#each_slice" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/enumerable/each_slice_spec.rb#L10
 
   it "passes element groups to the block" do
     acc = []
-    @enum.each_slice(3){|g| acc << g}.should be_nil
+    @enum.each_slice(3){|g| acc << g}
     acc.should == @sliced
   end
 
@@ -27,7 +27,7 @@ describe "Enumerable#each_slice" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/enumerable/each_slice_spec.rb#L27
 
   it "tries to convert n to an Integer using #to_int" do
     acc = []
-    @enum.each_slice(3.3){|g| acc << g}.should == nil
+    @enum.each_slice(3.3){|g| acc << g}
     acc.should == @sliced
 
     obj = mock('to_int')
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index 4674f984ffb..702c332cd22 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -731,6 +731,8 @@ class TestEnumerable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L731
     ary.clear
     (1..10).each_slice(11) {|a| ary << a}
     assert_equal([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], ary)
+
+    assert_equal(1..10, (1..10).each_slice(3) { })
   end
 
   def test_each_cons
@@ -750,6 +752,8 @@ class TestEnumerable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L752
     ary.clear
     (1..5).each_cons(6) {|a| ary << a}
     assert_empty(ary)
+
+    assert_equal(1..5, (1..5).each_cons(3) { })
   end
 
   def test_zip
-- 
cgit v1.2.1


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

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