[#1551] Hashes as keys — "Nathaniel Talbott" <nathaniel@...>

I was just playing around with Hash#hash and discovered that you can't use a

13 messages 2003/09/23

Re: Hashes as keys

From: Jim Freeze <jim@...>
Date: 2003-09-23 22:16:22 UTC
List: ruby-core #1552
On Wednesday, 24 September 2003 at  6:21:33 +0900, Nathaniel Talbott wrote:
> I was just playing around with Hash#hash and discovered that you can't use a
> Hash as a key in a hash:
> 
>   h = {{} => 'a'}
>   p h[{}]
> 
>   => nil
> 
> Is this intentional? Since hash overrides #==, I expected it to do the same
> with #hash, but it appears that it just uses Object#hash, which is different
> for every instance.
> 
> Thanks,
> 

irb
irb(main):001:0> h={}
=> {}
irb(main):002:0> hh = {:fred=>2}
=> {:fred=>2}
irb(main):003:0> h[hh] = 3
=> 3
irb(main):004:0> h
=> {{:fred=>2}=>3}
irb(main):005:0> h[{}] = 4
=> 4
irb(main):006:0> h
=> {{:fred=>2}=>3, {}=>4}

You mean like that?

-- 
Jim Freeze
----------
Campus sidewalks never exist as the straightest line between two
points.
		-- M. M. Johnston

In This Thread