On Wed, 2002-03-13 at 14:27, Vladimir Dozen wrote: > 2. How should I change the following code to get my object back from > Hash? > > ===================== > class Key > include Comparable > > def initialize(a,b) > @a = a > @b = b > end > > attr_reader :a, :b > > def <=> (another) > @a <=> another.a || @b <=> another.b > end > end > > hsh = Hash.new > key1 = Key.new(1,2) > key2 = Key.new(1,2) > > # the keys are considered equal, but... > hsh[key1] = "hello" > print "Value by separate key:",hsh[key2],"\n" > ===================== > > I always get nil. Being C++ programmer, I expected comparision > operator will be used to match the keys inside hash; I was wrong? From what I understand you were wrong... You need to create a Key#hash method.. : [bagfors / detrius]$ ; ri Object#hash ------------------------------------------------------------ Object#hash obj.hash -> aFixnum ------------------------------------------------------------------------ Generates a Fixnum hash value for this object. This function must have the property that a.eql?(b) implies a.hash == b.hash. The hash value is used by class Hash. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used. What's the best way to create such a hash-method btw?? /Erik -- Erik BéČfors | erik / bagfors.nu Supporter of free software | GSM +46 733 279 273 fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32