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
#<Foo:0x402568ec @s="blah", @i=1>
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?