From: Ryan Leavengood Date: 2001-11-03T09:15:37+09:00 Subject: [ruby-talk:24240] Re: Too much eval evil? (tell me why I shouldn't do this) > Let's say I've got a string which has some ruby variables in it and I > want each of those variables to take on a range of values and then I > want to evaluate that string given every combination of those values > (a cartesian product). And of course I want to be able to use this > with a variable number of varying variables, so my cartesian product > function has to work given any number of variables. This was just too interesting of a challenge for me to pass up. Here is my version (with no evaling): # I chose the name "cascade" since we are cascading down through the array def cascade(a, list = [], &block) return list if list.size == a.size a[list.size].each do |item| list << item cascade(a, list, &block) if list.size == a.size block.call(list) end list.slice!(-1) end list end a = [(0..20), (43..150), ['z','yz','xyz','xz','zz']] cascade(a) do |list| puts "width is: #{list[0]}, length is: #{list[1]}, hight is: #{list[2]}" end This runs about twice as fast as the one Thomas originally wrote. It could be probably be cleaned up, but it seems to work pretty well. Using an array instead of a hash guarantees the order in which the variables will be evaluated. Being able to call a block gives you more flexibility than just evaling a string. Also, when dealing with ranges you don't really need to turn them into arrays...they respond to "each" correctly. Ryan Leavengood