Actions
Bug #15849
closedUsing hash as first positional argument, mixed with named arguments causes unexpected behavior
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-linux]
Description
If the first argument of a method defaults to an empty hash, and there is another keyword argument after it, it does not set first parameter given to the first argument
For example:
data = {}
# This method does not modify the data hash as expected
def add_to_data(data = {}, key: 'a')
data[key] = 'value'
end
add_to_data(data)
puts data # {}
Trying the following produces an error:
data = { b: 'val' }
add_to_data(data) # ERROR: unknown keyword: b (ArgumentError)
There are a lot of other similar combinations of this method definition that do work as expected. It appears that having a default value for that first positional argument is what causes issues; see the attached script.
Files
Updated by jeremyevans0 (Jeremy Evans) almost 5 years ago
Updated by ko1 (Koichi Sasada) over 4 years ago
- Status changed from Open to Closed
I believe Jeremy :) and if it is not the same issue, please reopen it.
Actions
Like0
Like0Like0