[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Confusion Over Keyword Arguments

Berger, Daniel

3/2/2006 3:58:00 PM

> -----Original Message-----
> From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org]
> Sent: Wednesday, March 01, 2006 4:46 PM
> To: ruby-talk ML
> Subject: Re: Confusion Over Keyword Arguments

<snip>

> |How about "=" for keyword arguments instead
> |(such as in python)?
>
> Unfortunately, assignments are legal in argument list in Ruby.
>
> matz.

That can be made to work, with the understanding that '=' in a method
call means 'keyword argument', not 'assignment', since there is no point
in doing an assignment in a method call.

# Method definition, '=' means assignment (of default value):
def foo(bar, baz = 3)
...
end

# Method call, '=' means keyword
foo(baz = 5, bar = 2)

For anyone who cares to read my past thoughts on the subject:

http://djberg96.livejournal.com/...
http://djberg96.livejournal.com/...
http://djberg96.livejournal.com/...
http://djberg96.livejournal.com/...

Regards,

Dan


3 Answers

Ara.T.Howard

3/2/2006 4:05:00 PM

0

dblack

3/2/2006 4:19:00 PM

0

Yuu

3/2/2006 9:22:00 PM

0

Daniel Berger wrote:
> Yukihiro Matsumoto wrote:
>>
>>
>> foo(1, foo:3) # (a=1, b={:foo=>3}) or (a=1,b=0,c=[{:foo=>3}])?
>
> Error. There's no 'foo' parameter. Passing a literal hash would
> require {}.
>
>> foo(1, 2, 8, c:5) # c={:c=>5}) or c=[8,{:c=>5}] or error?
>
> Hm. Either an error or c = [5], depending on whether or not your want
> to
> declare that, once a positional is used, there's no going back after the
> fact.
>
>> args = [1, {:b=>2, foo=>5}]
>> foo(*args) # (a=1,b={:b=>2, foo=>5}) or (a=1,b=2,c=[{:foo=>5}])?
>
> a = 1, b = {:b=>2, foo=>5}, since you've passed a literal hash.
>
> Here's the latest test case we had for Sydney. Note that Sydney assumes
> that
> all method arguments automatically become keyword arguments. There's no
> syntax
> for explicitly declaring that a given method parameter is a valid
> keyword argument.
>
> For the kids following along at home, also see
> http://redhanded.hobix.com/inspect/namedParametersArenTTheyAll...
> for
> further discussion on the subject.
>
> Regards,
>
> Dan

Slightly off-topic but I found this part curious:

> # You can use named parameters, using the name of the parameter
defined in
> # in the method itself. It must be the name of the parameter,
followed by
> # a colon, followed by the value. Spaces between the parameter
name, colon
> # and value are not allowed.

Why is this? foo: bar would seem to be more legible with the
added benefit of no confusion with the scope operator :: nor
Symbols.

Your testcases do not seem to cover the scope completely and
Symbols are not included at all. Maybe add something like

def test_corner_cases()
# Symbols
assert_nothing_raised {@foo.bar x:1, y:2, z::symbol}

# Scopes
assert_nothing_raised {@foo.bar A::B, y:2, z:3}
assert_nothing_raised {@foo.bar x:1, y:2, z:::Object}

assert_nothing_raised{ @foo.bar(z:1, x:2, y:3) }
end # test_corner_cases

To cover those.

> < snip rest of test cases />


E

--
Posted via http://www.ruby-....