Filecrossjoin.rb
This content was produced by an LLM and could include errors.
このスクリプトは複数ファイルの行をクロス結合する想定のコードです。ARGVで渡された各パスを readlines(chomp: true) で行配列に読み込み、first.product(*rest) にブロックを渡して全組み合わせをカンマ区切りで出力します。ただし現状は発表用に ("a".."b") などのハードコードデータで上書きされており、実行すると 2×2×2 の8行が出力されます。
def read_lines(path)
File.readlines(path, chomp: true)
end
#=> :read_lines
arrays = ARGV.map { |path| read_lines(path) }
#=> []
#abort "Please specify at least one file." if arrays.empty?
#=> nil
first, *rest = arrays
#=> []
first, *rest = ("a".."b").to_a, 2.times.to_a, ("x".."y").to_a # for presentation only (remove later)
#=> [["a", "b"], [0, 1], ["x", "y"]]
first.product(*rest) do |*row|
puts row.join(",")
end
a,0,x
a,0,y
a,1,x
a,1,y
b,0,x
b,0,y
b,1,x
b,1,y
#=> ["a", "b"]
Ruby 4.0.6