Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method · added in Ruby 3.2

Enumerator.product

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 · 4.0 · 3.4 · 3.3