From: Kent Dahl Date: 2002-03-15T22:16:11+09:00 Subject: Re: key comparision inside Hash Vladimir Dozen wrote: > All I want is that default Object::hash() method return same value for > two objects having same number and names of object member variables, > and same values of those member variables. For simple cases like above > Coord class (where only @x and @y present) it would be enough, for > complex classes programmer will have to write own hash(). That kind of default behaviour sounds like it might have worse performance. I'd rather see it as an available module, ready for inclusion into custom classes. Seeing your lovely functional requirement, I couldn't help myself, so I had a stab at it: module NaiveHashable def hash inst = instance_variables h = type.hash ^ inst.size.hash inst.each{|i| h ^= i.hash ^ eval(i).hash } h end end class Key include NaiveHashable def initialize (x,y) @x, @y = x,y end def eql? (rhl) @x <=> rhl.x || @y <=> rhl.y end attr_reader :x, :y end key1 = Key.new(1,2) key2 = Key.new(1,2) print "hash1=#{key1.hash} hash1=#{key2.hash}\n" #=> hash1=537847700 hash1=537847700 > I guess, it will not break things? Are hash values relied upon for anything but performance? I haven't the foggiest about how Ruby handles hash value clashes. -- <[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]> )__(stud.techn.; ind. econ & management: computer technology)__( /"Opinions expressed are mine and not those of my Employer, "\ ( "the University, my girlfriend, stray cats, banana fruitflies, " ) \"nor the frontal lobe of my left cerebral hemisphere. "/