[ruby-core:98489] [Ruby master Bug#16908] Strange behaviour of Hash#shift when used with `default_proc`.
From:
merch-redmine@...
Date:
2020-05-23 02:53:36 UTC
List:
ruby-core #98489
Issue #16908 has been updated by jeremyevans0 (Jeremy Evans).
ioquatix (Samuel Williams) wrote in #note-2:
> @jeremyevans0 I agree with your assessment, however that hash does not have default_proc so I assume that it would return `nil` after all key-value pairs are `shift`ed out. That's different from invoking `default_proc` and returning `[nil, 0]`.
Same principle applies when using a default_proc:
```ruby
# simple hash with indifferent access
hash = Hash.new{|h,k| h[k.to_s] unless k.is_a? String}
hash['a'] = 1
hash['b'] = 2
while (k,v = hash.shift)
p [k, v]
end
```
----------------------------------------
Bug #16908: Strange behaviour of Hash#shift when used with `default_proc`.
https://bugs.ruby-lang.org/issues/16908#change-85765
* 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/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>