From: wconrad@... Date: 2002-05-04T07:02:58+09:00 Subject: Re: dynamically assigning instance variables This way, which is probably not the best way, seems to do something: wconrad@pluto:~/lab/foo1$ cat foo.rb #!/usr/bin/ruby class Foo def initialize(hash) for key, value in hash do eval "@#{key}=#{value.inspect}" end end end p Foo.new({"i"=>1, "s"=>"blah"}) wconrad@pluto:~/lab/foo1$ ./foo.rb # Wayne Conrad On Sat, May 04, 2002 at 06:43:10AM +0900, Berger, Daniel wrote: > Hi all, > > Yep, still trying to unwrap my brain from Perl and re-wrap it in Ruby... > > Is there a way to dynamically assign instance variables? > > class SomeClass > def initialize(hash) > hash.each do |key,val| > @#{key} = val # Doesn't work > end > end > end > > Is there a better way to do this?