From: Albert Wagner Date: 2002-02-17T05:36:29+09:00 Subject: Re: hash as key in hash On Saturday 16 February 2002 01:11 pm, you wrote: > After going through all this, you might want to extend the Hash class > and override hash() with these calls: > > class HashWhichYouCanReliablyUseAsKeyInAHash < Hash > def hash > return sort.to_s > end > end > > And create your hash indexes by creating new instances of the new class. Have I misunderstood something? It still doesn't work: class MyHash < Hash def hash str = '' self.sort.each do | k, v | str << (k * v) end str end end hash_a = MyHash.new hash_a['a'] = 1 hash_a['b'] = 2 hash_a['c'] = 3 hash_b = MyHash.new hash_b['c'] = 1 hash_b['b'] = 3 arg_hash = MyHash.new arg_hash['c'] = 1 arg_hash['b'] = 3 hash = { hash_a => 1, hash_b => 2} puts "#{hash_a.hash} is hash of hash_a" #==>abbccc puts "#{hash_b.hash} is hash of hash_b" #==>bbbc puts "#{arg_hash.hash} is hash of arg_hash" #==>bbbc puts "hash.key?(arg_hash) ==> #{hash.key?(arg_hash)}" #==>false -- Quantum Mechanics: The dreams stuff is made of