# Hash behavior query

**URL:** <https://rubytalk.org/t/hash-behavior-query/37967>\
**Category:** ruby-talk\
**Created:** [30 May 2007 11:44 UTC](https://rubytalk.org/t/hash-behavior-query/37967 "2007-05-30T11:44:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhisek\_Datta](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@Abhisek\_Datta](https://rubytalk.org/u/Abhisek_Datta)\
**Post date:** [30 May 2007 11:44 UTC](https://rubytalk.org/t/hash-behavior-query/37967/1 "2007-05-30T11:44:34Z")

</div>

irb(main):001:0\> d = [1,2,3,4]  
=\> [1, 2, 3, 4]  
irb(main):002:0\> h = Hash.new  
=\> {}  
irb(main):003:0\> h[d] = "hello"  
=\> "hello"  
irb(main):004:0\> h  
=\> {[1, 2, 3, 4]=\>"hello"}  
irb(main):005:0\> h[d]  
=\> "hello"  
irb(main):006:0\> d.push(5)  
=\> [1, 2, 3, 4, 5]  
irb(main):007:0\> h[d]  
=\> nil  
irb(main):008:0\> h  
=\> {[1, 2, 3, 4, 5]=\>"hello"}  
irb(main):009:0\> d  
=\> [1, 2, 3, 4, 5]  
irb(main):010:0\> h[d]  
=\> nil  
irb(main):011:0\> h.rehash  
=\> {[1, 2, 3, 4, 5]=\>"hello"}  
irb(main):012:0\> h[d]  
=\> "hello"  
irb(main):013:0\>

Is this expected or a bug?

-abhisek

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [30 May 2007 11:53 UTC](https://rubytalk.org/t/hash-behavior-query/37967/2 "2007-05-30T11:53:08Z")

</div>

Hi --

> **···**
>
> On Wed, 30 May 2007, Abhisek Datta wrote:
> 
> > irb(main):001:0\> d = [1,2,3,4]  
> > =\> [1, 2, 3, 4]  
> > irb(main):002:0\> h = Hash.new  
> > =\> {}  
> > irb(main):003:0\> h[d] = "hello"  
> > =\> "hello"  
> > irb(main):004:0\> h  
> > =\> {[1, 2, 3, 4]=\>"hello"}  
> > irb(main):005:0\> h[d]  
> > =\> "hello"  
> > irb(main):006:0\> d.push(5)  
> > =\> [1, 2, 3, 4, 5]  
> > irb(main):007:0\> h[d]  
> > =\> nil  
> > irb(main):008:0\> h  
> > =\> {[1, 2, 3, 4, 5]=\>"hello"}  
> > irb(main):009:0\> d  
> > =\> [1, 2, 3, 4, 5]  
> > irb(main):010:0\> h[d]  
> > =\> nil  
> > irb(main):011:0\> h.rehash  
> > =\> {[1, 2, 3, 4, 5]=\>"hello"}  
> > irb(main):012:0\> h[d]  
> > =\> "hello"  
> > irb(main):013:0\>
> > 
> > Is this expected or a bug?
> 
> You've pretty much reproduced the documented example from the source  
> code, so I'd say it's expected 🙂
> 
> David
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [30 May 2007 11:58 UTC](https://rubytalk.org/t/hash-behavior-query/37967/3 "2007-05-30T11:58:15Z")

</div>

... and furthermore was there not somebody asking whether to prefer  
Strings or Symbols as hash keys.  
I think the example above gives you some hints, -- sometimes Strings  
are still a good choice, but only sometimes.

Cheers  
Robert

> **···**
>
> On 5/30/07, dblack@wobblini.net \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Wed, 30 May 2007, Abhisek Datta wrote:
> > 
> > \> irb(main):001:0\> d = [1,2,3,4]  
> > \> =\> [1, 2, 3, 4]  
> > \> irb(main):002:0\> h = Hash.new  
> > \> =\> {}  
> > \> irb(main):003:0\> h[d] = "hello"  
> > \> =\> "hello"  
> > \> irb(main):004:0\> h  
> > \> =\> {[1, 2, 3, 4]=\>"hello"}  
> > \> irb(main):005:0\> h[d]  
> > \> =\> "hello"  
> > \> irb(main):006:0\> d.push(5)  
> > \> =\> [1, 2, 3, 4, 5]  
> > \> irb(main):007:0\> h[d]  
> > \> =\> nil  
> > \> irb(main):008:0\> h  
> > \> =\> {[1, 2, 3, 4, 5]=\>"hello"}  
> > \> irb(main):009:0\> d  
> > \> =\> [1, 2, 3, 4, 5]  
> > \> irb(main):010:0\> h[d]  
> > \> =\> nil  
> > \> irb(main):011:0\> h.rehash  
> > \> =\> {[1, 2, 3, 4, 5]=\>"hello"}  
> > \> irb(main):012:0\> h[d]  
> > \> =\> "hello"  
> > \> irb(main):013:0\>  
> > \>  
> > \> Is this expected or a bug?
> > 
> > You've pretty much reproduced the documented example from the source  
> > code, so I'd say it's expected 🙂
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw
