Named Arguments

(Kevin Olbrich) #1

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) #2

Here is how you can set the instance variable @test

class Test
attr_accessor :test

def initialize(args)
@test = args[:test] unless args[:test].nil?
end
end

t = Test.new({:test=>'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 = TestGeneric.new({:test=>'test two', :another_test=>'test two again'})
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:

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 Schröder) #3

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 do not know of such a feature and can't find it mentioned in the
pickaxe. Maybe you have a python pickaxe?
Otherwise, could you point me to the page where you found it?

regards,
Brian

···

On 23/08/05, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:

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

_Kevin

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

(Brian Takita) #4

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

···

On 8/22/05, Brian Takita <brian.takita@gmail.com> wrote:

Here is how you can set the instance variable @test

class Test
attr_accessor :test

def initialize(args)
@test = args[:test] unless args[:test].nil?
end
end

t = Test.new({:test=>'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 = TestGeneric.new({:test=>'test two', :another_test=>'test two again'})
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:
>
> 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
>
>
>

(Jeremy Tregunna) #5

Just as a supplementary note, the hash literals aren't required. You would get the same result with:

t = Test.new(:test=>'test one')

···

On 23-Aug-05, at 12:32 AM, Brian Takita wrote:

t = Test.new({:test=>'test one'})

--
Jeremy Tregunna

"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra

(Randy Kramer) #6

Pgs 84 and 85 of Pickaxe 2 discuss / describe hash arguments consisting of key
=> value pairs (and discussed as a workaround for keyed (named) arguments. I
suspect the OP was thinking of these.

Randy Kramer

···

On Tuesday 23 August 2005 05:23 am, Brian Schröder wrote:

I do not know of such a feature and can't find it mentioned in the
pickaxe. Maybe you have a python pickaxe?
Otherwise, could you point me to the page where you found it?

(Kevin Olbrich) #7

Actually the section that got me working on this is page 256. Of course at
1 am, I didn't notice that this was in the "windows automation" chapter, so
I guess I shouldn't have expected it to work in standard use.

—- it looks like there are a number of hacks and work-arounds to get a
feature *like* this to work, but from what I can tell Ruby doesn't do true
named arguments.

_Kevin

···

-----Original Message-----
From: Randy Kramer [mailto:rhkramer@gmail.com]
Sent: Tuesday, August 23, 2005 07:15 AM
To: ruby-talk ML
Subject: Re: Named Arguments

On Tuesday 23 August 2005 05:23 am, Brian Schröder wrote:

I do not know of such a feature and can't find it mentioned in the
pickaxe. Maybe you have a python pickaxe? Otherwise, could you point
me to the page where you found it?

Pgs 84 and 85 of Pickaxe 2 discuss / describe hash arguments consisting of
key
=> value pairs (and discussed as a workaround for keyed (named) arguments.
I
suspect the OP was thinking of these.

Randy Kramer

(Brian Takita) #8

from what I can tell Ruby doesn't do true named arguments.

Ruby does doesn't have a true implementation named arguments, that I know
of, but in practice, the Hash comes close.
For example Rails makes frequent use of named parameters, which are just
keys to a Hash.

Here is an example where using the hash is different:

def test(id, params)
if params[:active] == true
puts params[:a]
puts params[:b]
end
end

test(1, :active=>true, :a=>1, :b=>2, ...)

The first param, id, must be the first argument, but the params Hash can
take up the rest of the arguments.

You would also have to do your own testing to see that certain keys are
defined, if they are required.

···

On 8/23/05, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:

Actually the section that got me working on this is page 256. Of course at
1 am, I didn't notice that this was in the "windows automation" chapter,
so
I guess I shouldn't have expected it to work in standard use.

—- it looks like there are a number of hacks and work-arounds to get a
feature *like* this to work, but from what I can tell Ruby doesn't do true
named arguments.

_Kevin

-----Original Message-----
From: Randy Kramer [mailto:rhkramer@gmail.com]
Sent: Tuesday, August 23, 2005 07:15 AM
To: ruby-talk ML
Subject: Re: Named Arguments

On Tuesday 23 August 2005 05:23 am, Brian Schröder wrote:
> I do not know of such a feature and can't find it mentioned in the
> pickaxe. Maybe you have a python pickaxe? Otherwise, could you point
> me to the page where you found it?

Pgs 84 and 85 of Pickaxe 2 discuss / describe hash arguments consisting of
key
=> value pairs (and discussed as a workaround for keyed (named) arguments.
I
suspect the OP was thinking of these.

Randy Kramer