What's String.hash

irb(main):038:0* x=“hello”
=> “hello”
irb(main):039:0> x.object_id
=> 69352346
irb(main):040:0> x.hash
=> 267522041
irb(main):041:0> y=“hello”.intern
=> :hello
irb(main):042:0> y.object_id
=> 246184
irb(main):043:0> y.hash
=> 246184

What’s the meanings of String.hash method?
does it have any relation to object_id? Thanks.

On Tue, Jan 5, 2010 at 9:56 PM, Ruby N. [email protected]
wrote:

irb(main):043:0> y.hash
=> 246184

What’s the meanings of String.hash method?
does it have any relation to object_id? Thanks.

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


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: Rick DeNatale - Developer - IBM | LinkedIn

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

On Wed, Jan 6, 2010 at 4:07 PM, Ryan D. [email protected]
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?

On Jan 6, 2010, at 6:04 PM, Eric C. wrote:

must

I’m new to Ruby and have been wondering what +…+ is. Does it mark
emphasis in some markup language?

yes, and if you’re using fastri (it’s a gem), you’ll get actual
highlighting rather than seeing the + markup (which is from the RDoc).

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Jan 5, 2010, at 18:56 , Ruby N. wrote:

irb(main):043:0> y.hash
=> 246184

What’s the meanings of String.hash method?
does it have any relation to object_id? Thanks.

You should be using ri to answer these questions:

501 % ri String.hash
------------------------------------------------------------ String#hash
str.hash => fixnum

 From Ruby 1.8

 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.

503 % ri Object.object_id
------------------------------------------------------- Object#object_id
obj.id => fixnum
obj.object_id => fixnum

 From Ruby 1.8