Project

General

Profile

Bug #15849

Using hash as first positional argument, mixed with named arguments causes unexpected behavior

Added by jsmartt (Jared Smartt) 2 months ago. Updated 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-linux]
[ruby-core:92647]

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

ruby_named_param_bug.rb (1.2 KB) ruby_named_param_bug.rb Script demonstrating the issue jsmartt (Jared Smartt), 05/14/2019 06:37 PM

History

Updated by jeremyevans0 (Jeremy Evans) 2 months ago

I believe this is the same issue as #12717. We are trying to address this issue in #14183.

Also available in: Atom PDF