Cartesian product of lists

def allpairs xs, ys
  result = []
  xs.each {|x| ys.each {|y|
    result << yield(x, y)
  }}
  result
end

p allpairs(['h','o','t'], ['d','o','g']){|x,y| x + y}
p allpairs([1, 2, 3, 5], [7, 11, 13]){|x,y| x * y}
["hd", "ho", "hg", "od", "oo", "og", "td", "to", "tg"]
[7, 11, 13, 14, 22, 26, 21, 33, 39, 35, 55, 65]

More lists?

def tripples xs, ys, zs
  result = []
  xs.each {|x| ys.each {|y| zs.each {|z| 
    result << yield(x, yield(y, z))
  }}}
  result
end

puts tripples(['A','B'],['i','j'],['1','2','3']){|a,b| a+'-'+b}.join(' ')
A-i-1 A-i-2 A-i-3 A-j-1 A-j-2 A-j-3 B-i-1 B-i-2 B-i-3 B-j-1 B-j-2 B-j-3