From: Aldric Giacomoni Date: 2009-12-19T01:57:39+09:00 Subject: Re: Unique combination of values from arrays Aldric Giacomoni wrote: > Giampiero Zanchi wrote: >> one = ['one', 'two'] >> two = ['three', 'four'] >> three = ['five', 'six'] Sorry for the spam, just wanted to repost the cleaner solutions: Mine first, of course, because it is ugly: def array_permutations array, index=0 # index is 0 by default : start at the beginning, more elegant. return array[-1] if index == array.size - 1 # Return last element if at end. result = [] array[index].each do |element| # For each array array_permutations(array, index + 1).each do |x| # Permute permute permute result << "#{element}, #{x}" end end return result end My adaptation of Giampiero's solution: def fancy_array_permutation array return array[0] if array.size == 1 first = array.shift return first.product( fancy_array_permutation(array) ).map {|x| x.flatten.join(" ")} end -- Posted via http://www.ruby-forum.com/.