From: Martin DeMello Date: 2005-08-08T17:01:08+09:00 Subject: Re: cartesian product walter a kehowski wrote: > > cartprod.rb:24: syntax error > (c ||= []) << ae,be > ^ > cartprod.rb:26: syntax error > cartprod.rb:132: syntax error My mistake, sorry. Should be (c ||= []) << [ae, be] > and I would like to take the cartesian product of an arbitrary number of > sets in the most elegant way. Define a cartprod2 that takes the cartesian product of two arrays. Define base cases sensibly - cartprod2([], a) = a.map {|i| [i]} and cartprod2(a,b) = a.each {|i| b.each {|j| c << i.concat(j)}} Use that as a helper function for cartprod thus: def cartprod(*arrays) arrays.inject([]) {|prod, ary| cartprod2(prod, ary)} end Untested, but that's the general idea. martin