Named Arguments

K

Kevin Olbrich

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
 
B

Brian Takita

------=_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

=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--
 
B

Brian Takita

------=_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


=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
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--
 
R

Robert Klemme

Brian said:
Here is yet another way (
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top