Skip to content
class method · added in Ruby 3.2

Enumerator.product

[Elem] (*::_EachEntry[Elem]) -> ::Enumerator::Product[Elem]
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 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

  1. 3.2
See also .produce #+ .new #each also in 4.1 · 3.4 · 3.3 · 3.2