From: Tom Willis Date: 2005-03-07T02:30:33+09:00 Subject: Noob:Objects as key in hash Hi all, I didn't want my first bit of participation on this list to be something this silly. But I'm afraid I missed something vital in my 48 hour crash course. If someone could take 10 seconds and tell me what I'm doing wrong I'd greatly appreciate it and will give you props in my epitaph. Feel free to point out and make fun of the style too :) I have thick skin. I want to use an object as a key in a Hash. here's some example code... def main puts "running this script..." testpointid end def testpointid p = PointID.new(1,1) p2 = PointID.new(1,2) pdup = PointID.new(1,1) print "p!=pdup\n" if p!=pdup d = Hash.new() d[p]="point1" d[p2]="point2" #both of these return nil and I don't understand why print "PointID test failed" if d[p]!="point1" print "PointID test failed" if d[pdup]!="point1" #this works, but it's not quite the interface I want d[p.hash]="point1" d[p2.hash]="point2" print "PointID test failed" if d[p.hash]!="point1" print "PointID test failed" if d[pdup.hash]!="point1" end class PointID attr_reader :x,:y def initialize(x,y) @x = x @y = y @x.freeze @y.freeze end def hash return [@x.object_id,@y.object_id].to_s end def ==(other) return hash == other.hash end def to_s return hash() end end -- Thomas G. Willis http://paperbackmusic.net