Bug #2764
Hash.new inconsistency when initialized with an enumerable
| Status: | Rejected | Start date: | 02/20/2010 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | - | % Done: | 0% |
|
| Category: | core | |||
| Target version: | - | |||
| ruby -v: | ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-darwin9] |
Description
When initializing a Hash with an enumerable as default value, Hash.new(obj) and Hash.new(&block) have different behaviours.
Example:
h = Hash.new([])
h[10] << 20
h #=> {}
h = Hash.new { |hash, key| hash[key] = [] }
hash[10] << 20
h #=> {10=>20}
The example similar when using a String instead of an Array as default value.
History
Updated by Marc-Andre Lafortune almost 2 years ago
- Category set to core
- Status changed from Open to Rejected
This is by design.
Note that the behavior is actually different for all types of objects, even immediates
h = Hash.new(42)
h[:foo] # ==> 42
h.keys # ==> []
h = Hash.new { |hash, key| hash[key] = 42 }
h[:foo] # ==> 42
h.keys # ==> [:foo]