class method · added in Ruby 3.2
Enumerator.product
[Elem] (*::_EachEntry[Elem]) -> ::Enumerator::Product[Elem]
Enumerator.product(*enums) → enumerator
Enumerator.product(*enums) { |elts| ... } → enumerator
enumerator.c
View on GitHub ↗
static VALUE
enumerator_s_product(int argc, VALUE *argv, VALUE klass)
{
VALUE enums = Qnil, options = Qnil, block = Qnil;
rb_scan_args(argc, argv, "*:&", &enums, &options, &block);
if (!NIL_P(options) && !RHASH_EMPTY_P(options)) {
rb_exc_raise(rb_keyword_error_new("unknown", rb_hash_keys(options)));
}
VALUE obj = enum_product_initialize(argc, argv, enum_product_allocate(rb_cEnumProduct));
if (!NIL_P(block)) {
enum_product_run(obj, block);
return Qnil;
}
return obj;
}
Generates a new enumerator object that generates a Cartesian product of given enumerable objects. This is equivalent to Enumerator::Product.new.
Example
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.
Ruby version changes
-
3.2
-
Enumerator.producthas been added.Enumerator::Productis the implementation. [Feature #18685]
-