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.
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)?
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)?
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
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)
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
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:
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.
In message “Re: Confusion Over Keyword Arguments”
on Thu, 2 Mar 2006 09:32:41 +0900, “E. Saynatkari” [email protected]
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…
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