[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash behavior query

Abhisek Datta

5/30/2007 11:45:00 AM

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

2 Answers

dblack

5/30/2007 11:53:00 AM

0

Robert Dober

5/30/2007 11:58:00 AM

0

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 :-)
... 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



--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw