Project

General

Profile

Actions

Bug #15849

closed

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

Added by jsmartt (Jared Smartt) almost 5 years ago. Updated over 4 years ago.

Status:
Closed
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

Updated by jeremyevans0 (Jeremy Evans) almost 5 years ago

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

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

Also available in: Atom PDF

Like0
Like0Like0