Jim Freeze [mailto:jim / freeze.org] wrote:

> 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?

Sure, you can assign a Hash as a Hash key, but it's mostly useless. For
instance:

  irb(main):001:0> h = {{} => 'a'}
  => {{}=>"a"}
  irb(main):002:0> p h[{}]
  nil
  => nil

The underlying question is, why do two Hashes with the same content have
different hash values?:

  irb(main):001:0> h1 = {}
  => {}
  irb(main):002:0> h2 = {}
  => {}
  irb(main):003:0> h1 == h2
  => true
  irb(main):004:0> [h1.hash, h2.hash]
  => [22880740, 22876600]

My guess is that Hash doesn't override Object#hash, but that of course
raises its own question :-)


Nathaniel

<:((><