Skip to content

Commit a5cff7c

Browse files
committed
Make Enumerable#each_cons return object if over size
This behavior changed in dfb47bb, 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.
1 parent d1cbec9 commit a5cff7c

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

enum.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,12 +2968,12 @@ enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj)
29682968
* returns +self+:
29692969
*
29702970
* a = []
2971-
* (1..10).each_slice(3) {|tuple| a.push(tuple) } # => nil
2971+
* (1..10).each_slice(3) {|tuple| a.push(tuple) }
29722972
* a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
29732973
*
29742974
* a = []
29752975
* h = {foo: 0, bar: 1, baz: 2, bat: 3, bam: 4}
2976-
* h.each_slice(2) {|tuple| a.push(tuple) } # => nil
2976+
* h.each_slice(2) {|tuple| a.push(tuple) }
29772977
* a # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]
29782978
*
29792979
* With no block given, returns an Enumerator.
@@ -3047,12 +3047,12 @@ enum_each_cons_size(VALUE obj, VALUE args, VALUE eobj)
30473047
* returns +self+:
30483048
*
30493049
* a = []
3050-
* (1..5).each_cons(3) {|element| a.push(element) } # => nil
3050+
* (1..5).each_cons(3) {|element| a.push(element) }
30513051
* a # => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
30523052
*
30533053
* a = []
30543054
* h = {foo: 0, bar: 1, baz: 2, bam: 3}
3055-
* h.each_cons(2) {|element| a.push(element) } # => nil
3055+
* h.each_cons(2) {|element| a.push(element) }
30563056
* a # => [[[:foo, 0], [:bar, 1]], [[:bar, 1], [:baz, 2]], [[:baz, 2], [:bam, 3]]]
30573057
*
30583058
* With no block given, returns an Enumerator.
@@ -3068,7 +3068,7 @@ enum_each_cons(VALUE obj, VALUE n)
30683068
if (size <= 0) rb_raise(rb_eArgError, "invalid size");
30693069
RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_cons_size);
30703070
arity = rb_block_arity();
3071-
if (enum_size_over_p(obj, size)) return Qnil;
3071+
if (enum_size_over_p(obj, size)) return obj;
30723072
memo = MEMO_NEW(rb_ary_new2(size), dont_recycle_block_arg(arity), size);
30733073
rb_block_call(obj, id_each, 0, 0, each_cons_i, (VALUE)memo);
30743074

test/ruby/test_enum.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ def test_each_slice
739739
assert_equal([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], ary)
740740

741741
assert_equal(1..10, (1..10).each_slice(3) { })
742+
assert_equal([], [].each_slice(3) { })
742743
end
743744

744745
def test_each_cons
@@ -760,6 +761,7 @@ def test_each_cons
760761
assert_empty(ary)
761762

762763
assert_equal(1..5, (1..5).each_cons(3) { })
764+
assert_equal([], [].each_cons(3) { })
763765
end
764766

765767
def test_zip

0 commit comments

Comments
 (0)