ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: merch-redmine@jeremyevans.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:98487] [Ruby master Bug#16908] Strange behaviour of Hash#shift when used with `default_proc`.
Date: Sat, 23 May 2020 02:26:04 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-85763.20200523022604.3344@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16908.20200523021553.3344@ruby-lang.org

Issue #16908 has been updated by jeremyevans0 (Jeremy Evans).


While your particular example is non-intuitive, there is a simple explanation for it. The first time `hash.shift` is called, `hash` is empty, so it returns the default value (`0`).  It gets the default value by calling the default_proc for `hash` with a `nil` key.  There is no better option since `hash.shift` isn't provided a key.  The second time `hash.shift` is called, the hash is not empty, so it returns the first entry as a key value pair.  I agree `Hash#shift` semantics with a default_proc are questionable, but I'm not sure if it could be improved.

I don't think we should change this behavior.  It is expected that `Hash.new.shift` should return nil, as should `Hash.new(nil).shift` and `Hash.new{}.shift`.

`hash.shift` is used in conditionals:

```ruby
hash = {a: 1, b: 2}
while (k,v = hash.shift)
  p [k, v]
end
```

If you change `Hash#shift` to return an array when the hash is empty, you've turned this into an infinite loop.

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

* 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/

  reply	other threads:[~2020-05-23  2:26 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 ` merch-redmine [this message]
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 ` [ruby-core:98492] " eregontp
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-85763.20200523022604.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).