# Hash.new({}) astonishment

**URL:** <https://www.ruby-forum.com/t/hash-new-astonishment/85769>\
**Category:** Ruby\
**Created:** [February 16, 2007, 4:57am UTC](https://www.ruby-forum.com/t/hash-new-astonishment/85769 "2007-02-16T04:57:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcello\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@Marcello\_B](https://www.ruby-forum.com/u/Marcello_B)\
**Post date:** [February 16, 2007, 4:57am UTC](https://www.ruby-forum.com/t/hash-new-astonishment/85769/1 "2007-02-16T04:57:46Z")

</div>

> > h = Hash.new({})  
> > =\> {}
> 
> > h[:a][:b] = 1  
> > =\> 1
> 
> > h  
> > =\> {}
> 
> > h[:a]  
> > =\> {:b=\>1}
> 
> > h.keys  
> > =\> []
> 
> > h.values  
> > =\> []

??

---

<div class="post-metadata">

**Author:** ![Marcello\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@Marcello\_B](https://www.ruby-forum.com/u/Marcello_B)\
**Post date:** [February 16, 2007, 5:26am UTC](https://www.ruby-forum.com/t/hash-new-astonishment/85769/2 "2007-02-16T05:26:22Z")

</div>

Marcello B. wrote:

> > > h = Hash.new({})  
> > > =\> {}  
> > > h[:a][:b] = 1  
> > > =\> 1  
> > > h  
> > > =\> {}  
> > > h[:a]  
> > > =\> {:b=\>1}  
> > > h.keys  
> > > =\> []  
> > > h.values  
> > > =\> []
> 
> ??

[http://www.ruby-doc.org/core/classes/Hash.src/M002873.html](http://www.ruby-doc.org/core/classes/Hash.src/M002873.html)  
Hash.new(obj) =\> aHash  
If obj is specified, this single object will be used for all default  
values.

irb(main):001:0\> h = Hash.new({})  
=\> {}  
irb(main):002:0\> h[:a] = 12345 # default object = 12345  
=\> 12345  
irb(main):003:0\> h[:b] = 45678 # default object = 45678  
=\> 45678  
irb(main):004:0\> h[:a]  
=\> 12345  
irb(main):005:0\> efine@ender:~/ruby/ckjm$ irb  
irb(main):001:0\> h = Hash.new({})  
=\> {}  
irb(main):002:0\> h[:a][:b] = 12345 # single default object now = 12345  
=\> 12345  
irb(main):003:0\> h  
=\> {}  
irb(main):004:0\> h[:xyzzy]  
=\> {:b=\>12345}  
irb(main):005:0\> h[0]  
=\> {:b=\>12345}  
irb(main):006:0\> h[0].object\_id == h[:xyzzy].object\_id  
=\> true  
irb(main):007:0\> h[109123].object\_id == h[:not\_there].object\_id  
=\> true  
irb(main):008:0\> h  
=\> {}  
irb(main):009:0\> h = Hash.new {|hash,key| hash[key] = {}}  
=\> {}  
irb(main):010:0\> h[:a][:b] = 12345  
=\> 12345  
irb(main):011:0\> h  
=\> {:a=\>{:b=\>12345}}  
irb(main):012:0\> h.keys  
=\> [:a]  
irb(main):013:0\> h.values  
=\> [{:b=\>12345}]

---

<div class="post-metadata">

**Author:** ![Marcello\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@Marcello\_B](https://www.ruby-forum.com/u/Marcello_B)\
**Post date:** [February 16, 2007, 5:27am UTC](https://www.ruby-forum.com/t/hash-new-astonishment/85769/3 "2007-02-16T05:27:55Z")

</div>

Sorry, middle button operator trouble 🙂

irb(main):001:0\> h = Hash.new({})  
=\> {}  
irb(main):002:0\> h[:a][:b] = 12345 # single default object now = 12345  
=\> 12345  
irb(main):003:0\> h  
=\> {}  
irb(main):004:0\> h[:xyzzy]  
=\> {:b=\>12345}  
irb(main):005:0\> h[0]  
=\> {:b=\>12345}  
irb(main):006:0\> h[0].object\_id == h[:xyzzy].object\_id  
=\> true  
irb(main):007:0\> h[109123].object\_id == h[:not\_there].object\_id  
=\> true  
irb(main):008:0\> h  
=\> {}  
irb(main):009:0\> h = Hash.new {|hash,key| hash[key] = {}}  
=\> {}  
irb(main):010:0\> h[:a][:b] = 12345  
=\> 12345  
irb(main):011:0\> h  
=\> {:a=\>{:b=\>12345}}  
irb(main):012:0\> h.keys  
=\> [:a]  
irb(main):013:0\> h.values  
=\> [{:b=\>12345}]

---

<div class="post-metadata">

**Author:** ![Marcello\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@Marcello\_B](https://www.ruby-forum.com/u/Marcello_B)\
**Post date:** [February 16, 2007, 7:25am UTC](https://www.ruby-forum.com/t/hash-new-astonishment/85769/4 "2007-02-16T07:25:54Z")

</div>

On 2/16/07, Marcello B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > h = Hash.new({})  
> > > =\> {}  
> > > h[:a][:b] = 1  
> > > You are assigning to the {}, gotten by the [:a]  
> > > look at this  
> > > irb#1(main):014:0\> h[:a][:b]=42  
> > > =\> 42  
> > > irb#1(main):015:0\> o  
> > > =\> {:b=\>42}  
> > > irb#1(main):016:0\> h  
> > > =\> {}  
> > > irb#1(main):017:0\>  
> > > irb#1(main):018:0\*  
> > > irb#1(main):019:0\* o={}  
> > > =\> {}  
> > > irb#1(main):020:0\> h=Hash.new(o)  
> > > =\> {}  
> > > irb#1(main):021:0\> h[:a][:b]=42  
> > > =\> 42  
> > > irb#1(main):022:0\> h[:c]=42  
> > > =\> 42  
> > > irb#1(main):023:0\> h  
> > > =\> {:c=\>42}  
> > > irb#1(main):024:0\> o  
> > > =\> {:b=\>42}

or maybe even better

h=Hash.new({})  
=\> {}  
irb#1(main):027:0\> h[:a][:b]=42  
=\> 42  
irb#1(main):028:0\> h.default  
=\> {:b=\>42}

It is completely normal that h is empty you were working with it’s  
default property

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![Marcello\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@Marcello\_B](https://www.ruby-forum.com/u/Marcello_B)\
**Post date:** [February 16, 2007, 9:17am UTC](https://www.ruby-forum.com/t/hash-new-astonishment/85769/5 "2007-02-16T09:17:04Z")

</div>

Hi,

On Friday 16 February 2007 05:26, Edwin F. wrote:

> If obj is specified, this single object will be used for all default  
> values.

yuck. i got it. thanks :).
