From: Tim Pease Date: 2007-08-21T00:13:05+09:00 Subject: Re: How to implement a hash whose key is another hash? On 8/20/07, acmeman1@yahoo.com wrote: > I understand that the key of a hash can be any object. Therefore, > I would think that the key of a hash can be another hash. If I have > the following code sequence. (I using ruby 1.8.5 (2006-08-25) [i486- > linux]) > > > require 'pp' > test ={} > test["new"=>"girl"]= 100 > test["new"=>"girl"]= 200 > pp test > > I am expecting the output to be > > {{"new"=>"girl"}=>200} > > However the output I am getting is > > {{"new"=>"girl"}=>100, > {"new"=>"girl"}=>200} > The problem is that you are not using the same key ... k = {'new' => 'girl'} k.hash #=> 1072992710 h = {} h[k] = 100 h[k] = 200 p h #=> {{"new" => "girl"} => 200} k = {'new' => 'girl'} k.hash #=> 1072934850 h[k] = 300 p h #=> {{"new"=>"girl"}=>300, {"new"=>"girl"}=>200} To use an object as a hash key, you need to implement a "hash" method that returns the same value when the contents of the hash are the same. But this gets tricky. Blessings, TwP