ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: eregontp@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:98492] [Ruby master Bug#16908] Strange behaviour of Hash#shift when used with `default_proc`.
Date: Sat, 23 May 2020 12:39:05 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-85768.20200523123904.3344@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16908.20200523021553.3344@ruby-lang.org

Issue #16908 has been updated by Eregon (Benoit Daloze).


In other words, the current semantics of `return hash.default(nil) if hash.empty?` feel hacky and actually harmful to me.
The user probably never expects to have the default_proc called with a `nil` key in many cases.

----------------------------------------
Bug #16908: Strange behaviour of Hash#shift when used with `default_proc`.
https://bugs.ruby-lang.org/issues/16908#change-85768

* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
* ruby -v: 2.7.0
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
I don't have any strong opinion about this, but I observed the following behaviour which I thought was confusing. Maybe it's okay, or maybe we should change it to be more consistent.

```
hash = Hash.new{|k,v| k[v] = 0}

hash.shift # => 0
hash.shift # => [nil, 0]
```

My feeling was, both cases should return `[nil, 0]`.




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

  parent reply	other threads:[~2020-05-23 12:39 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-23  2:15 [ruby-core:98486] [Ruby master Bug#16908] Strange behaviour of Hash#shift when used with `default_proc` samuel
2020-05-23  2:26 ` [ruby-core:98487] " merch-redmine
2020-05-23  2:38 ` [ruby-core:98488] " samuel
2020-05-23  2:53 ` [ruby-core:98489] " merch-redmine
2020-05-23 12:37 ` [ruby-core:98491] " eregontp
2020-05-23 12:39 ` eregontp [this message]
2020-05-24  0:53 ` [ruby-core:98493] " daniel
2021-03-17  5:31 ` [ruby-core:102900] " matz
2021-09-27  5:37 ` [ruby-core:105439] " ioquatix (Samuel Williams)
2021-12-03  2:23 ` [ruby-core:106430] " naruse (Yui NARUSE)
2021-12-27 22:46 ` [ruby-core:106855] " jeremyevans0 (Jeremy Evans)
2022-01-01  7:12 ` [ruby-core:106938] " ioquatix (Samuel Williams)
2022-01-14  3:01 ` [ruby-core:107113] " mame (Yusuke Endoh)
2022-01-15  1:39 ` [ruby-core:107135] " ioquatix (Samuel Williams)

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-85768.20200523123904.3344@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).