Make Enumerartor.product return nil when called with a block

This commit is contained in:
Akinori MUSHA 2022-12-21 19:13:15 +09:00
parent 308ccbaeb2
commit cc4c28ec2e
2 changed files with 10 additions and 3 deletions

View File

@ -3725,6 +3725,7 @@ enum_product_inspect(VALUE obj)
/*
* call-seq:
* Enumerator.product(*enums) -> enumerator
* Enumerator.product(*enums) { |elts| ... } -> enumerator
*
* Generates a new enumerator object that generates a Cartesian
* product of given enumerable objects. This is equivalent to
@ -3733,6 +3734,9 @@ enum_product_inspect(VALUE obj)
* e = Enumerator.product(1..3, [4, 5])
* e.to_a #=> [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
* e.size #=> 6
*
* When a block is given, calls the block with each N-element array
* generated and returns +nil+.
*/
static VALUE
enumerator_s_product(int argc, VALUE *argv, VALUE klass)
@ -3747,9 +3751,12 @@ enumerator_s_product(int argc, VALUE *argv, VALUE klass)
VALUE obj = enum_product_initialize(argc, argv, enum_product_allocate(rb_cEnumProduct));
if (NIL_P(block)) return obj;
if (!NIL_P(block)) {
enum_product_run(obj, block);
return Qnil;
}
return enum_product_run(obj, block);
return obj;
}
/*

View File

@ -964,7 +964,7 @@ class TestEnumerator < Test::Unit::TestCase
# with a block
elts = []
ret = Enumerator.product(1..3) { |x| elts << x }
assert_instance_of(Enumerator::Product, ret)
assert_equal(nil, ret)
assert_equal [[1], [2], [3]], elts
assert_equal elts, Enumerator.product(1..3).to_a