From: "Florian Groß" Date: 2005-05-05T23:41:16+09:00 Subject: Re: Whats so different about a Hash? Andrew Walrond wrote: > Is Hash#to_a guaranteed to sort the elements? My tests show that it does, but > perhaps .to_a.sort might be safer? I think that two hashes with the same elements will also have the same order. This might however not be true anymore in the future. (There were talks about retaining insert order.) So calling .sort might indeed be a good idea. > So I propose > > class Hash > def hash() > [self.to_s.sort,self.class].hash > end > def eql?(other) > self.hash === other.hash > end > end Be careful about defining eql?() in terms of hash(). hash values will and can produce collisions and Ruby will always use eql?() even after the hash codes match to check if the elements are indeed the same. If you implement eql?() in terms of hash() you will however get false positives in case of collisions.