From: karoyakani Date: 2007-01-29T14:45:05+09:00 Subject: Re: cartesian product of arrays How about recursive version? module Enumerable def cartprod(*args) return self if [] == args b = args.shift result = [] self.each do |n| b.cartprod(*args).each do |a| result << [n] + (a.kind_of?(Array)? a: [a]) end end result end end def cartprod(*args) args.shift.cartprod(args) end Then both forms work as below: p (1..2).cartprod((3..5), (6..8)) p cartprod([1,2],[3,4,5],[6,7,8]) Further consideration would be to use a block for output formating: e.g. replace result << [n] + a above to yield n, a and a block {|n,a| n +a.join(' ')} for string concatination etc FYI, TJ On Jan 27, 10:14 am, Peter Szinek wrote: > Thomas Hafner wrote: > > Peter Szinek wrote/schrieb <45B9E90C.7010...@rubyrailways.com>: > > >> Just a suggestion: it is also possible to reopen Enumerable and put your > >> code there - in this case, all enumerable types will have this > >> functionality (e.g. Sets), not only Arrays. > > > Does that follow your suggestion?:Exactly! > > Cheers, > Peter > > __http://www.rubyrailways.com