Crossjoin.rb

This content was produced by an LLM and could include errors.

このスクリプトは Array#product による直積(クロス結合)です。3文字 × 2数値 × 1ハッシュの配列を掛け合わせ、["a", 0, {}] のような6通りのタプルを一度に生成します。SQLの CROSS JOIN 相当を純粋な配列操作で実現できます。

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, {}]]

Ruby 4.0.6