From: Andrew Johnson Date: 2004-03-03T01:54:48+09:00 Subject: Re: dynamic assignment of instance variables On 2 Mar 2004 08:16:20 -0800, Gerard A.W. Vreeswijk wrote: [snip] > Is it possible to set instances variables without eval or string > interpolation? I can see a couple of ways, so long as the right symbol is passed: To set instance variables, pass in :@instance_variable symbols and use #instance_variable_set(sym,val). class Person def initialize(args) args.each do |sym,val| instance_variable_set(sym,val) end end end p Person.new(:@name => 'John', :@born => '2003') Using accessors, pass in the :method= symbol and use send(sym,val): class Person attr_accessor :name, :born def initialize(args) args.each do |k,v| send(k,v) end end end p Person.new(:name= => 'John', :born= => '2003') # or if you prefer: p Person.new(:name==> 'Fred', :born==> '2004') regards, andrew -- Andrew L. Johnson http://www.siaris.net/ It's kinda hard trying to remember Perl syntax *and* Occam's razor at the same time :-) -- Graham Patterson