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

ruby-changes:44269

From: marcandre <ko1@a...>
Date: Wed, 5 Oct 2016 03:25:35 +0900 (JST)
Subject: [ruby-changes:44269] marcandRe: r56342 (trunk): * enum.c: Make Enumerable#chunk with no block return

marcandre	2016-10-05 03:25:16 +0900 (Wed, 05 Oct 2016)

  New Revision: 56342

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56342

  Log:
    * enum.c: Make Enumerable#chunk with no block return
      an Enumerator [#2172]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/enum.c
    trunk/test/ruby/test_enum.rb
Index: NEWS
===================================================================
--- NEWS	(revision 56341)
+++ NEWS	(revision 56342)
@@ -42,6 +42,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L42
 
   * Enumerable#sum  [Feature #12217]
   * Enumerable#uniq  [Feature #11090]
+  * Enumerable#chunk called without a block now return an Enumerator
+    [Feature #2172]
 
 * Enumerator::Lazy
 
Index: test/ruby/test_enum.rb
===================================================================
--- test/ruby/test_enum.rb	(revision 56341)
+++ test/ruby/test_enum.rb	(revision 56342)
@@ -611,6 +611,12 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L611
 
     e = @obj.chunk {|elt| :_foo }
     assert_raise(RuntimeError) { e.to_a }
+
+    e = @obj.chunk.with_index {|elt, i| elt - i }
+    assert_equal([[1, [1, 2, 3]],
+                  [-2, [1, 2]]], e.to_a)
+
+    assert_equal(4, (0..3).chunk.size)
   end
 
   def test_slice_before
Index: enum.c
===================================================================
--- enum.c	(revision 56341)
+++ enum.c	(revision 56342)
@@ -2995,14 +2995,14 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yield https://github.com/ruby/ruby/blob/trunk/enum.c#L2995
  *      }
  *    }
  *
+ *  If no block is given, an enumerator to `chunk` is returned instead.
  */
 static VALUE
 enum_chunk(VALUE enumerable)
 {
     VALUE enumerator;
 
-    if (!rb_block_given_p())
-	rb_raise(rb_eArgError, "no block given");
+    RETURN_SIZED_ENUMERATOR(enumerable, 0, 0, enum_size);
 
     enumerator = rb_obj_alloc(rb_cEnumerator);
     rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56341)
+++ ChangeLog	(revision 56342)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Oct  5 03:24:55 2016  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* enum.c: Make Enumerable#chunk with no block return
+	  an Enumerator [#2172]
+
 Wed Oct  5 01:19:45 2016  NAKAMURA Usaku  <usa@r...>
 
 	* internal.h (ST2FIX): new macro to convert st_index_t to Fixnum.

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

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