From: Robert Klemme Date: 2007-05-09T18:05:11+09:00 Subject: Re: Equality question On 08.05.2007 18:55, Brian Candler wrote: > On Wed, May 09, 2007 at 12:58:01AM +0900, Sebastian Hungerecker wrote: >> Brian Candler wrote: >>> o1 = [:defuzz_configs, {"conf"=>"foo.txt"}] >>> o2 = [:defuzz_configs, {"conf"=>"foo.txt"}] >>> p o1 == o2 # prints true >>> p o1.hash == o2.hash # prints false ?? <<<<<<<<<<< >> That's because the Hashs in o1 and o2 are two different objects. > > So hashes and arrays don't behave the same in this regard? > > o1 = ["hello", ["world"]] > o2 = ["hello", ["world"]] > p o1 == o2 > p o1.hash == o2.hash # prints true > > o1 = {"key"=>"hello"} > o2 = {"key"=>"hello"} > p o1 == o2 > p o1.hash == o2.hash # prints false > > I wonder why this is? Probably because Hashes store not only key value pairs but also default value and an optionally block that is invoked if an element is missing from the Hash. Now, blocks are difficult to compare in itself but the question here is: on what basis do you consider Hashes equivalent? Do you include the default object or not etc. As far as I remember this has been discussed on the list in the past to some extent. The solution is of course to use your own class for your keys. Since they seem to be pretty complex that's a good idea anyway. Kind regards robert