From: Timothy Hunter Date: 2006-10-13T09:18:37+09:00 Subject: Re: Hash & keys... S辿bastien Wautelet wrote: > I'm having some trouble understanding how hashes work in Ruby. Consider > the following test : > > irb(main):002:0> v=Vector[4,5] > => Vector[4, 5] > irb(main):003:0> v2=Vector[4,5] > => Vector[4, 5] > irb(main):004:0> v==v2 > => true > irb(main):005:0> v.hash > => 17 > irb(main):006:0> v2.hash > => 17 > irb(main):007:0> h=Hash.new > => {} > irb(main):008:0> h[v]="whatever is in (4,5)" > => "whatever is in (4,5)" > irb(main):009:0> h[v2] > => nil > > I'd have expected h[v2] => "whatever is in (4,5)", since v==v2 and both > have the same hash value... what am I doing wrong? > > It's not enough that they have the same hash value or that obj1 == obj2. For the purposes of Hash, two objects are the same if obj1.eql?(obj2). Does v1.eql?(v2)?