From: Brian Takita Date: 2005-08-23T13:32:30+09:00 Subject: Re: Named Arguments ------=_Part_825_25094961.1124771547399 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Here is how you can set the instance variable @test class Test attr_accessor :test def initialize(args) @test =3D args[:test] unless args[:test].nil? end end t =3D Test.new({:test=3D>'test one'}) p t.test This way is more generic way to set the instance variables: class TestGeneric attr_accessor :test, :another_test def initialize(args) args.each do |k, v| self.instance_variable_set("@#{k.to_s}", v) end end end t =3D TestGeneric.new({:test=3D>'test two', :another_test=3D>'test two agai= n'}) p t.test p t.another_test Of course, there may be a better way to do this. :Brian On 8/22/05, Kevin Olbrich wrote: >=20 > I was working through the new version of the pickaxe tonight and decided= =20 > to > try the 'named arguments' feature. The book suggests that one can simply > pass a hash with the names of local variables and they with automagically > get set to the hash values. >=20 > I was not able to get this to work. Am I missing something here, or is=20 > this > feature not working as described? >=20 > _Kevin >=20 >=20 > ------=_Part_825_25094961.1124771547399--