From: Gary Boone Date: 2006-12-08T12:28:39+09:00 Subject: Re: [Puzzle] How to do parameter variation? Cool. While I study the others, here's my solution. Looks like it's similar to Greg's. --Gary # expand_hash_lists # # note that accumulating lists must be passed in as the first argument # def expand_hash_lists(a1, a2) a1.inject([]){|acc,f| a2.inject(acc){|a,b| a << [b]+ (f.is_a?(Hash)?[f]:f)}} end if __FILE__ == $0 # tests of expand_hash_lists ah=[{'a'=>1}, {'a'=>2}, {'a'=>3}] bh=[{'b'=>21}, {'b'=>22}, {'b'=>23}] ch=[{'c'=>31}, {'c'=>32}, {'c'=>33}] dh=[{'d'=>41}, {'d'=>42}, {'d'=>43}] abh = expand_hash_lists(ah, bh) abch = expand_hash_lists(abh, ch) p expand_hash_lists(abch, dh).inspect # Additional tests p "----" p [[{'a'=>1}], [{'b'=>2}]].inject{|acc, vl| expand_hash_lists(acc, vl)}.inspect p "----" p [[{'a'=>1}, {'a'=>3}], [{'b'=>2}]].inject{|acc, vl| expand_hash_lists(acc, vl)}.inspect p "----" p [[{'a'=>1}, {'a'=>3}], [{'b'=>2}, {'b'=>4}]].inject{|acc, vl| expand_hash_lists(acc, vl)}.inspect end -- Posted via http://www.ruby-forum.com/.