From: Brian Candler Date: 2010-01-07T00:49:00+09:00 Subject: Re: what's String.hash A Hash puts objects into "hash chains", to make it quick to find a particular key amongst a very large collection. Object#hash gives a number, which after a little more processing points directly to the right hash chain. You then only have to search along that one chain to find the object. So it's not really that eql? is expensive, but rather than you want to minimise the number of eql?'s that are done, by only looking along one chain. > for example > comparing two large arrays or strings which have many initial elements > equal requires scanning until either the end of one of them or until > an unequal element is found. But calculating a hash value for an array or string also involves iterating through the whole object, so that argument doesn't really hold. For example: to ensure that the following two arrays have the same hash, Array#hash is having to calculate the hash of each element, and combine those hashes together to make the final value. >> a = [1,2,"foo"] => [1, 2, "foo"] >> b = [1,2,"foo"] => [1, 2, "foo"] >> a.hash => 876516209 >> b.hash => 876516209 If I mutate one of the objects contained, the hash changes (which shows that it's being recalculated across the whole array) >> b[2] << "x" => "foox" >> b.hash => -1093306170 -- Posted via http://www.ruby-forum.com/.