From: Mr Pinto Date: 2006-08-12T18:40:34+09:00 Subject: Re: newbie question: how to create variable --Apple-Mail-1-974714423 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On Aug 11, 2006, at 12:55 PM, Robert Klemme wrote: > Karel Michek wrote: >> Hi, >> how to create variable in the method that accepts string as name >> of the variable and the object? Something like this: >> class InterpreterCallback def set_object(name, value) >> #do something here > > instance_variable_set("@#{name}", value) >> end >> end >> $callback = InterpreterCallback.new() >> $callback.set_object("var", 3) >> puts var >>>>> 3 Depending on what you're trying to do, you might be able to use a simple eval statement: irb(main):001:0> name="var" => "var" irb(main):002:0> value="3" => "3" irb(main):003:0> eval "#{name} = value" => "3" irb(main):004:0> var => "3" --Apple-Mail-1-974714423--