I'm currently using Ruby 2.1.0p0.
The "problem" can be simplified a little with the following example:
def foo(arg1 = {}, arg2: 'value1')
[arg1, arg2]
end
Here, the method foo
has one OPTIONAL argument arg1
(with default {}
) and one OPTIONAL keyword argument, arg2
.
If you call:
foo({key1: 'value1'})
You get the error:
ArgumentError: unknown keyword: key1
from (irb):17
from /home/mark/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'
The reason is that Ruby is attempting to match the only argument you gave (with keyword key1
) to the only OPTIONAL keyword argument which is keyword arg2
. They don't match, thus the error.
In the next example:
foo({val1: 'value', val2: 'value2'}, arg2: 1)
We get the result:
=> [{:val1=>"value", :val2=>"value2"}, 1]
This makes sense because I provided two arguments. Ruby can match arg2: 1
to the second keyword argument and accepts {val1: 'value', val2: 'value2'}
as a substitute for the first optional argument.
I do not consider the behaviors above a bug.