Forum: Ruby Confusion Over Keyword Arguments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4c8a9bec5a27b66b28d3c5cddeb70e93?d=identicon&s=25 Mr. Big (Guest)
on 2006-03-01 21:32
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)?
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-01 21:47
(Received via mailing list)
On Mar 1, 2006, at 3:32 PM, Mr. Big wrote:

>
> 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-forum.com/.
>


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
4c8a9bec5a27b66b28d3c5cddeb70e93?d=identicon&s=25 Mr. Big (Guest)
on 2006-03-01 22:11
> 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)
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-03-01 22:40
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
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2006-03-01 22:44
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-01 22:52
(Received via mailing list)
Hi --

On Thu, 2 Mar 2006, Jeff Cohen wrote:

> 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 :-)

I don't think it's extra.  I would hate to have to parse -- visually
-- things like:

   hash = { a = 1, b = 2, 4 = 5 }

etc.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-03-01 22:55
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?
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-03-01 23:00
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-01 23:00
(Received via mailing list)
Hi --

On Thu, 2 Mar 2006, Jeff Cohen wrote:

>  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 }

No one said *illegal* assignments were allowed :-)

But consider this:

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


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-03-01 23:08
David Black wrote:
> No one said *illegal* assignments were allowed :-)

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

Jeff
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2006-03-01 23:17
(Received via mailing list)
Quoting Jeff Cohen <cohen.jeff@gmail.com>:

>   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
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-03-02 00:47
(Received via mailing list)
Hi,

In message "Re: Confusion Over Keyword Arguments"
    on Thu, 2 Mar 2006 05:32:50 +0900, "Mr. Big" <no@no.com> writes:

|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?

No, on the virtual implementation (only inside of my brain), they are
same things.  The magic is in receiving and interpreting arguments.

|Why must keyword arguments use the same
|syntax as new hash literals?

No, we don't have to.  The colon can be ugly with symbols, for
example,

  db.find(order: :date)

is not good looking.  Any other proposal (except for "=')?

|How about "=" for keyword arguments instead
|(such as in python)?

Unfortunately, assignments are legal in argument list in Ruby.

							matz.
D111305c32e46f7dd2794a956208d347?d=identicon&s=25 E. Saynatkari (Guest)
on 2006-03-02 01:32
Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: Confusion Over Keyword Arguments"
>     on Thu, 2 Mar 2006 05:32:50 +0900, "Mr. Big" <no@no.com> writes:
>
> |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?
>
> No, on the virtual implementation (only inside of my brain), they are
> same things.  The magic is in receiving and interpreting arguments.
>
> |Why must keyword arguments use the same
> |syntax as new hash literals?
>
> No, we don't have to.  The colon can be ugly with symbols, for
> example,
>
>   db.find(order: :date)
>
> is not good looking.  Any other proposal (except for "=')?

We are running out of characters, could we instead switch to
be the very first fully Unicode language? Have the actual
lambda sign for lambdas, the sigil for symbols and so on.. :)

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


E
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-03-02 01:40
(Received via mailing list)
Hi,

In message "Re: Confusion Over Keyword Arguments"
    on Thu, 2 Mar 2006 09:32:41 +0900, "E. Saynatkari" <none@none.net>
writes:

|We are running out of characters, could we instead switch to
|be the very first fully Unicode language? Have the actual
|lambda sign for lambdas, the sigil for symbols and so on.. :)

Too late.  Perl6 took that place.

							matz.
A63764f318f10379c8b51349b757cf4b?d=identicon&s=25 Jay Levitt (Guest)
on 2006-03-02 02:13
(Received via mailing list)
On Thu, 2 Mar 2006 08:46:23 +0900, Yukihiro Matsumoto wrote:

> No, we don't have to.  The colon can be ugly with symbols, for
> example,
>
>   db.find(order: :date)
>
> is not good looking.  Any other proposal (except for "=')?

If the keyword has to be unadorned, and this has to be done solely
through
an infix operator, how about:

db.find (order ~> :date)
db.find (order -> :date)

If it's ok to adorn the keyword, and the parser can distinguish these,
that
opens up lots of options:

db.find ([order] = :date)
db.find (|order| = :date)
db.find (-order- = :date)
db.find (/order/ = :date)
db.find (#order# = :date)
db.find (#order = :date)

I've got no clue how the parser works, so these may be non-starters for
various reasons.

Jay Levitt
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-03-02 02:15
(Received via mailing list)
On 1-Mar-06, at 7:40 PM, Yukihiro Matsumoto wrote:

> Too late.  Perl6 took that place.
Does Perl 6 exist yet?  (Yes I know Pugs exists, but Perl 6 seems to
always be about two years away ;-)

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2006-03-02 05:38
(Received via mailing list)
I don't think anything works like ':' does --it has a divisor quality.
So as long as a symbol is denotated with a prefix ':' there's the
symptom of non-intuitiveness involved.

So short of changing the symbol prefix --I was just playing with a
space+period idea, in fact

  db.find( order: .date )

How does allowing

  db.find( order::date )

and deprecating ::'s previous meaning, work out?

T.
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-03-02 06:20
(Received via mailing list)
On 3/1/06, Jay Levitt <jay+news@jay.fm> wrote:
> an infix operator, how about:
>
> db.find (order ~> :date)
> db.find (order -> :date)
>

How about using Pascal for inspiration? Keep both the : folks and the
= crowd happy...

db.find (order := :date)


-Adam
This topic is locked and can not be replied to.