what's String.hash

R

Ruby Newbee

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.
 
R

Rick DeNatale

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.

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: http://www.linkedin.com/in/rickdenatale
 
B

Brian Candler

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
 
R

Ryan Davis

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.

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
------------------------------------------------------------------------
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+.
 
E

Eric Christopherson

502 % ri Object.hash
------------------------------------------------------------ Object#hash
=A0 =A0 obj.hash =A0 =A0=3D> fixnum

=A0 =A0 From Ruby 1.8
st

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

Rob Biedenharn

On Wed, Jan 6, 2010 at 4:07 PM, Ryan Davis <ryand-


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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top