ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: ko1@atdot.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:93968] [Ruby master Bug#15849] Using hash as first positional argument, mixed with named arguments causes unexpected behavior
Date: Mon, 29 Jul 2019 07:01:27 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-80159.20190729070126.2df458d5af13f5c8@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15849.20190514183938@ruby-lang.org

Issue #15849 has been updated by ko1 (Koichi Sasada).

Status changed from Open to Closed

I believe Jeremy :) and if it is not the same issue, please reopen it.


----------------------------------------
Bug #15849: Using hash as first positional argument, mixed with named arguments causes unexpected behavior
https://bugs.ruby-lang.org/issues/15849#change-80159

* Author: jsmartt (Jared Smartt)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
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:

``` ruby
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:

```ruby
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)


-- 
https://bugs.ruby-lang.org/

      parent reply	other threads:[~2019-07-29  7:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15849.20190514183938@ruby-lang.org>
2019-05-14 18:39 ` [ruby-core:92647] [Ruby trunk Bug#15849] Using hash as first positional argument, mixed with named arguments causes unexpected behavior jared_smartt_92
2019-05-14 18:51 ` [ruby-core:92648] " merch-redmine
2019-07-29  7:01 ` ko1 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-80159.20190729070126.2df458d5af13f5c8@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).