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