summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--NEWS2
-rw-r--r--enum.c4
-rw-r--r--test/ruby/test_enum.rb6
4 files changed, 15 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index b6e5dbbad6..5f496a555a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Oct 5 03:24:55 2016 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * enum.c: Make Enumerable#chunk with no block return
+ an Enumerator [#2172]
+
Wed Oct 5 01:19:45 2016 NAKAMURA Usaku <usa@ruby-lang.org>
* internal.h (ST2FIX): new macro to convert st_index_t to Fixnum.
diff --git a/NEWS b/NEWS
index ed373f3b34..571eb57a13 100644
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,8 @@ with all sufficient information, see the ChangeLog file or Redmine
* Enumerable#sum [Feature #12217]
* Enumerable#uniq [Feature #11090]
+ * Enumerable#chunk called without a block now return an Enumerator
+ [Feature #2172]
* Enumerator::Lazy
diff --git a/enum.c b/enum.c
index 84f6a6f6af..625097c636 100644
--- a/enum.c
+++ b/enum.c
@@ -2995,14 +2995,14 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
* }
* }
*
+ * 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);
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index c87c60ac09..4ff4f88ca9 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -611,6 +611,12 @@ class TestEnumerable < Test::Unit::TestCase
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