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