From: Robert Klemme Date: 2007-07-24T18:07:36+09:00 Subject: Re: Variable Generation 2007/7/23, Ari Brown : > yes, I mean spontaneously generated variables. > > Assume we have an array: > array = %w(yea cool awesome stuff) > Now, I'm looking to make a variable corresponding to the item. so that: > > yea = "yea" > cool = "cool" > awesome = "awesome" > stuff = "stuff" > > My current method (which FAILS) is using eval > array.each {|item| eval(item + "=\"#{item}\"") } > > I tested this with a puts instead of eval, and it comes out exactly > as it should. But when I try to use the variable, I get a > undefined local variable or method > error. There are various issues to your approach, namely that you cannot reference local variables that are defined in an eval block and not defined in the code around it. Ruby needs to read the variable name literally if you want do access it. You cannot do something like some_magic_that_sets_x() puts x Your probably is probably better solved by either using a Hash or using OpenStruct. Kind regards robert