Forum: Ruby Hash behavior query

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B377914eb25c288ba77475f191618a7e?d=identicon&s=25 Abhisek Datta (Guest)
on 2007-05-30 13:45
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-05-30 13:54
(Received via mailing list)
Hi --

On Wed, 30 May 2007, Abhisek Datta wrote:

> irb(main):006:0> d.push(5)
> => {[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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-30 13:58
(Received via mailing list)
On 5/30/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
> > irb(main):004:0> h
> > => [1, 2, 3, 4, 5]
>
> 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
This topic is locked and can not be replied to.