From: Tom Willis Date: 2005-03-07T07:00:32+09:00 Subject: Re: Noob:Objects as key in hash On Mon, 7 Mar 2005 05:48:47 +0900, Jim Weirich wrote: > On Sunday 06 March 2005 01:47 pm, James Britt wrote: > > Jim Weirich wrote: > > > def hash > > > @x.object_id + @y.object_id > > > end > > > > Isn't this the same value as > > > > @y.object_id + @x.object_id > > Yes. As Joel points out that to some degree you expect things to occasionally > hash the the same value. If too many things hash to the same value, then > your hash array will slow down. > > For small boards, it probably doesn't matter, but not only do [1,2] and [2,1] > map to the same hash, but so would [0,3] and [3,0]. In fact, all diagnols > running from the lower left to the upper right have the same hash value on > all their cells. > > So x+y suggestion, while working, is rather suboptimal. How about this ... > > def hash > @x + 1000*@y > end > > (I dropped the object_id call because it looks like we are mapping from > fixnums). > > -- > -- Jim Weirich jim@weirichhouse.org http://onestepback.org > ----------------------------------------------------------------- > "Beware of bugs in the above code; I have only proved it correct, > not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas) > > LOL I should learn to read. or get a bigger monitor or glasses. I've been pulling my hair out for the last few hours trying to get it to work with Jim's suggestions of using eq? only to compose a lengthy self-deprecating rantish reply. Only to discover, Jim said the eql? operator. So, I'm to new to know the difference just yet. But I got it working. Here's the code in all it's glory for future noobs sake. #---ruby proggie class PointID attr_reader :x,:y def initialize(x,y) @x = x @y = y freeze end def hash return [@x.object_id ,@y.object_id].hash end def ==(other) result = self.hash == other.hash #result = (self.x == other.x)and (self.y == other.y) return result end def eql?(other) return self == other end def to_s return "#{@x},#{@y}" end end #---end proggie Thanks again for all the feedback. Hopefully someday I can return the favor. -- Thomas G. Willis http://paperbackmusic.net