From: Joel VanderWerf Date: 2005-03-07T05:05:54+09:00 Subject: Re: Noob:Objects as key in hash James Britt wrote: > Jim Weirich wrote: > >> ... >> The hash function should return an integer to be used for hashing. >> Your version returns a string. One correction might be ... >> >> def hash >> [@x.object_id, @y.object_id].to_s.hash >> end >> >> That will now work. But you probably really don't want to bother >> converting the above to a string. Why not just do: >> >> def hash >> @x.object_id + @y.object_id >> end > > > Isn't this the same value as > > @y.object_id + @x.object_id > > giving, for example, > > PointID.new( 1, 2).hash == PointID.new( 2, 1).hash > > > But they are different coordinates. That's ok, as long as #eql? says they are different. In fact, many objects must give the same value in response to #hash, since there are far more possible objects than Fixnums. The #hash method is only used to find the hash bin, and then #eql? is used to test equality within the bin.