Actually all Objects have both a hash and an eql? method.
They're there to support the implementation of the Hash class, and
there's a strict definition of the relationship between those two
methods.
Hash uses the eql? method to determine if two keys are considered the
same. The Object class makes eql? the same as ==, but some Objects
override this, for example, 1 == 1.0 returns true, but 1.eql?(1.0)
does not.
For some objects eql? might be somewhat expensive, 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.
So the hash method is used to avoid the Hash object needing to compare
everything. It must return a fixnum. The definition of Object#hash
states that any overriding implementation must ensure that
self.eql?(another) implies self.hash == other.hash
Comparing two fixnums for equality is very fast, so when a Hash object
wants to determine if it contains a particular object as a key, it
compares the hash of that object to the hash of each of its keys. If
the two hash values aren't == then the key and the object can't be
eql? so the key can be rejected as a match. The potentially expensive
comparison using eql? is only done when the hash of the object is ==
to the hash of a key in the hash. There's a bit more to it than that,
the fixnum hash values can also be used to compute where to look for a
key in the hash table, but the basic idea is that the hash value is a
quick test to rule out two objects being eql?
HTH
···
On Tue, Jan 5, 2010 at 9:56 PM, Ruby Newbee <rubynewbee@gmail.com> wrote:
What's the meanings of String.hash method?
does it have any relation to object_id? Thanks.
------------------------------------------------------------------------
Return a hash based on the string's length and content.
502 % ri Object.hash
------------------------------------------------------------ Object#hash
obj.hash => fixnum
From Ruby 1.8
------------------------------------------------------------------------
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.
From Ruby 1.8
------------------------------------------------------------------------
Returns an integer identifier for _obj_. The same number will be
returned on all calls to +id+ for a given object, and no two active
objects will share an id. +Object#object_id+ is a different concept
from the +:name+ notation, which returns the symbol id of +name+.
Replaces the deprecated +Object#id+.
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)
On Jan 6, 2010, at 6:04 PM, Eric Christopherson wrote:
On Wed, Jan 6, 2010 at 4:07 PM, Ryan Davis <ryand- > ruby@zenspider.com> wrote:
502 % ri Object.hash
------------------------------------------------------------ Object#hash
obj.hash => fixnum
From Ruby 1.8
------------------------------------------------------------------------
Generates a +Fixnum+ hash value for this object. This function must
I'm new to Ruby and have been wondering what +...+ is. Does it mark
emphasis in some markup language?