From: David Alan Black Date: 2001-11-03T08:44:28+09:00 Subject: [ruby-talk:24233] Re: Too much eval evil? (tell me why I shouldn't do this) Hello -- On Sat, 3 Nov 2001, Thomas Gandy wrote: > I've been doodling with Ruby (experimenting with it in order to figure > out what it's capable of) like a kid with crayons... ;-) > > So here I present some thoughts and I request a critique. > > 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. > > Since you get a cartesian product of a fixed number of arrays by > doing: > #say we have three arrays: > a_ary.each { |a| > b_ary.each { |b| > c_ary.each { |c| > puts "a=#{a}, b=#{b}, c=#{c} > } > } > } > > That's all well and good if you know up front that you'll only have > three arrays to deals with, but if you don't.... My idea was to > create the cartesian product function dynamically and to an > instance_eval of it. I present the code below. Please let me know if > this is a crazy idea or not and also present alternatives: I don't have a full-blown alternative, but just a suggestion: have you looked at the Matrix class? (matrix.rb in the Ruby distribution) I'm not sure whether it is relevant, but you might have a look. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav