From: Morton Goldberg Date: 2007-07-24T06:52:00+09:00 Subject: Re: Variable Generation On Jul 23, 2007, at 5:19 PM, Ari Brown wrote: > 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. Your problem likely results from the scoping rules for local variables. I think it would work as you expect if you used global or instance variables. However, for this kind of thing, I usually prefer a hash. You might consider using something like: WORDS = %w(yea cool awesome stuff) DICTIONARY = {} WORDS.each { |w| DICTIONARY[w.to_sym] = w } DICTIONARY[:cool] # => "cool" Regards, Morto