From: John Joyce Date: 2007-05-09T09:58:46+09:00 Subject: Re: Equality question On May 9, 2007, at 1:55 AM, 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? > it's worth exploring further! (the ruby cookbook has a good section on ruby's hashes) I made two hashes like yours. irb(main):011:0> o1.__id__ => 1611054 irb(main):012:0> o2.__id__ => 1586104 irb(main):013:0> o1.hash => 1611054 irb(main):014:0> o2.hash => 1586104 As you can see the two hashes don't share the same object id, .hash method of objects creates a hash value for the object (confusing terminology, I know) but with hashes , that hash value just happens to be the same as the object id. They may contain (references to) the same objects, so that makes these two hashes look a lot alike, but they're as different as you and your clone. What's more confusing? Try messing with symbols in your hashes. Then wrap your head around it. I finally got a handle on symbols. They're like hard links in unix. little wormholes if you will.