Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Named Arguments (http://www.velocityreviews.com/forums/t823803-named-arguments.html)

Kevin Olbrich 08-23-2005 04:08 AM

Named Arguments
 
I was working through the new version of the pickaxe tonight and decided 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.

I was not able to get this to work. Am I missing something here, or is this
feature not working as described?

_Kevin




Brian Takita 08-23-2005 04:32 AM

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 <kevin.olbrich@duke.edu> 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--



Brian Takita 08-23-2005 05:14 AM

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...uby-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 <brian.takita@gmail.com> 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 <kevin.olbrich@duke.edu> 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--



Robert Klemme 08-23-2005 07:03 AM

Re: Named Arguments
 
Brian Takita wrote:
> Here is yet another way (
> http://blade.nagaokaut.ac.jp/cgi-bin...uby-talk/52331) to
> do this:
>
> class AnotherTest
> attr_accessor :test, :another_test
>
> def initialize(args)
> @test, @another_test = args.values_at(:test, :another_test)
> end
> end
>
> t = AnotherTest.new({:test=>'test two', :another_test=>'test two
> again'}) p t.test
> p t.another_test


Note that you don't need the curly braces, which makes stuff look a bit
better:

t = AnotherTest.new(:test=>'test two', :another_test=>'test two')

Kind regards

robert



All times are GMT. The time now is 08:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.