From: David Masover Date: 2010-05-17T07:52:15+09:00 Subject: Re: iterating over sub arrays On Sunday, May 16, 2010 05:19:05 pm James Harrison wrote: > <-----code-----> > array1 = ["0", "1", "2", "3"] > array2 = ["jam", "bees", "please"] > array3 = ["6", "7", "8"] > > array1.each do |a| > array2.each do |b| > array3.each do |c| > puts "#{a} #{b} #{c}" > end > end > end > <-----/code-----> > > The output of that little snippet is all of the possible iterations of the > three arrays. > > I want to generalise this, such that if I have an arbitrary length array of > arrays: > > arrayCollection = [["0", "1", "2", "3"], ["jam", "bees", "please"], ["6", > "7", "8"]] Minor nit: by convention, Ruby people tend to use CamelCase for constants only, and underscores for variables. Call it array_collection. Anyway, seems like one obvious way would be recursion: def each_join array, context=[], &block if array.length == 0 yield context else first = array.first rest = array[1...array.length] first.each do |elem| each_join rest, context+[elem], &block end end end Not pretty, and I'm sure someone could improve it, but it works. It does seem pretty weird, though. Out of curiosity, what do you need this for?