Hash behavior query

A

Abhisek Datta

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
 
D

dblack

Hi --

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)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Dober

Hi --

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top