[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Confusion Over Keyword Arguments

nope

3/1/2006 8:33:00 PM

Ruby 2.0 will include new syntax for hash literals: {a:3, b:4}. However,
one can leave off the {}s to create a hash. Current software uses this
for "faked" keyword arguments.

def my_meth(options={})
end

(1) my_meth(:keyword => 3) # Ruby 1.8
(2) my_meth(keyword:3, another_option:10) # Ruby 2.0

Won't this create confusion? Why must keyword arguments use the same
syntax as new hash literals? How about "=" for keyword arguments instead
(such as in python)?

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


17 Answers

Logan Capaldo

3/1/2006 8:47:00 PM

0


On Mar 1, 2006, at 3:32 PM, Mr. Big wrote:

> Ruby 2.0 will include new syntax for hash literals: {a:3, b:4}.
> However,
> one can leave off the {}s to create a hash. Current software uses this
> for "faked" keyword arguments.
>
> def my_meth(options={})
> end
>
> (1) my_meth(:keyword => 3) # Ruby 1.8
> (2) my_meth(keyword:3, another_option:10) # Ruby 2.0
>
> Won't this create confusion? Why must keyword arguments use the same
> syntax as new hash literals? How about "=" for keyword arguments
> instead
> (such as in python)?
>
> --
> Posted via http://www.ruby-....
>


Ruby doesn't have keyword arguments at all. When you call a method
with "keyword arguments" you are really just passing in a single
hash. There's no confusion because the syntaxes do the exact same thing
example_hash = { a: 1, b: 2 }
my_meth(example_hash) #pass a hash as the single argument to my_meth
my_meth(a: 1, b: 2) # pass a hash as the single argument to my_meth

Neither of these will actually set local variables, the signature for
a method like this is as follows:

def my_meth(argument_hash)
...
end

e.g.:

def my_meth(arg_hash)
arg_hash[:a] + arg_hash[:b]
end

So one would say my_meth(:a => 1, :b => 2) and get back 3
or one would say my_meth(a: 1, b: 2) and get back 3
or one would say
example_hash = { :a => 1, :b => 2 }
my_meth(example_hash) # returns 3 also



nope

3/1/2006 9:11:00 PM

0

> When you call a method with "keyword arguments" you are really just passing in a single hash.

Keyword arguments aren't hashes.

def my_meth(foo:, bar:)
foo + bar
end
my_meth(foo:3, bar:4)

# later we see this, we do not know how the method is defined.
my_meth2(arg:100, arg2:10)

There is no way to tell if my_meth2 is using a hash or keyword arguments
without looking it up. However, if keyword arguments used "=" instead,
there would be no double checking.
my_meth2(arg=100, arg2=10)

A keyword argument that takes a symbol ends up looking very ugly (I
guess "=" doesn't work well here, either):
foo(key::symbol) or
foo(key: :symbol)

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


Jeff

3/1/2006 9:40:00 PM

0

Mr. Big wrote:
> Ruby 2.0 will include new syntax for hash literals: {a:3, b:4}. However,
> one can leave off the {}s to create a hash.
>
> Won't this create confusion?

I am personally not in favor of allowing the : character instead of =>.
(The last time I checked, this new syntax was marked as highly
experimental and not a done deal yet).

With colons already in use to denote the start of a symbol name, using a
colon as a separator between a key-value pair is going to confuse many
people, especially since everyone seems to like their own whitespace
conventions.

I hope Matz reconsiders and decides that the "experiment" is not worth
it.

As an aside, I wonder why => was chosen instead of a simple = sign.
Seems like it's un-ruby-like to make us type the extra character :-)

Jeff
www.softiesonrails.com


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


MenTaLguY

3/1/2006 9:44:00 PM

0

Quoting Jeff Cohen <cohen.jeff@gmail.com>:

> As an aside, I wonder why => was chosen instead of a simple =
> sign.
> Seems like it's un-ruby-like to make us type the extra character
> :-)

Assignments are legal in most places that => is; would you want to
trade one for the other?

-mental


dblack

3/1/2006 9:52:00 PM

0

Jeff

3/1/2006 9:56:00 PM

0

unknown wrote:
> Quoting Jeff Cohen <cohen.jeff@gmail.com>:
>
> Assignments are legal in most places that => is; would you want to
> trade one for the other?
>
> -mental

Really? This works for me:

h = { 'a' => 5, 'b' => 6 }

but this gives my syntax errors:

h = { 'a' = 5, 'b' = 6 }

test.rb:1: odd number list for Hash
h = { 'a' = 5, 'b' = 6 }
^
test.rb:1: syntax error
h = { 'a' = 5, 'b' = 6 }
^

Maybe I'm doing something wrong here?

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


dblack

3/1/2006 10:01:00 PM

0

Jeff

3/1/2006 10:01:00 PM

0

David Black wrote:
> I don't think it's extra. I would hate to have to parse -- visually
> -- things like:
>
> hash = { a = 1, b = 2, 4 = 5 }

That's true. I guess I was thinking of method calls, like:

start_hockey_game(:home_team = "Chicago", :away_team = "Detroit")

In other words, in reality most of the hashes I create are implicit
hashes created when passing method arguments.

But you're right, for your canonical case, it would be bad; and that
alone probably justifies having a separate syntax for the key-value
pair.

Jeff

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


Jeff

3/1/2006 10:09:00 PM

0

David Black wrote:
> No one said *illegal* assignments were allowed :-)

Phhhewwwwww - Man, has it been a long day. Sorry for the brainfreeze.
:-)

Jeff



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


MenTaLguY

3/1/2006 10:17:00 PM

0

Quoting Jeff Cohen <cohen.jeff@gmail.com>:

> unknown wrote:
> > Quoting Jeff Cohen <cohen.jeff@gmail.com>:
> >
> > Assignments are legal in most places that => is; would you want
> > to trade one for the other?
> >
> > -mental
>
> Really? This works for me:
>
> h = { 'a' => 5, 'b' => 6 }
>
> but this gives my syntax errors:
>
> h = { 'a' = 5, 'b' = 6 }

Legal, not equivalent.

For example, you can do this:

p { 'a' => k = 5, 'b' => 6 }
p k

which would print:

{"a"=>5, "b"=>6}
5

The arguments to => can be any legal Ruby expressions, including
assignments.

-mental