Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

crossjoin.rb(join)

The product method is then called on the a, b, and c arrays. This method returns an array of all possible combinations of the elements in the input arrays. In this case, the resulting array will contain 3 elements, each with a combination of the elements in the a, b, and c arrays.

Execution:

a = ('a'..'c').to_a
#=> ["a", "b", "c"]
b = 2.times.to_a
#=> [0, 1]
c = [{}]
#=> [{}]

a.product(b, c)
#=> 
[["a", 0, {}],
 ["a", 1, {}],
 ["b", 0, {}],
 ["b", 1, {}],
 ["c", 0, {}],
 ["c", 1, {}]]

Executed with Ruby 3.3.5