# What's String.hash

**URL:** <https://www.ruby-forum.com/t/whats-string-hash/181135>\
**Category:** Ruby\
**Created:** [January 6, 2010, 3:57am UTC](https://www.ruby-forum.com/t/whats-string-hash/181135 "2010-01-06T03:57:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 6, 2010, 3:57am UTC](https://www.ruby-forum.com/t/whats-string-hash/181135/1 "2010-01-06T03:57:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 6, 2010, 4:16am UTC](https://www.ruby-forum.com/t/whats-string-hash/181135/2 "2010-01-06T04:16:58Z")

</div>

On Tue, Jan 5, 2010 at 9:56 PM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 6, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/whats-string-hash/181135/3 "2010-01-06T16:48:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 7, 2010, 12:05am UTC](https://www.ruby-forum.com/t/whats-string-hash/181135/4 "2010-01-07T00:05:11Z")

</div>

On Wed, Jan 6, 2010 at 4:07 PM, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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?

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 7, 2010, 12:42am UTC](https://www.ruby-forum.com/t/whats-string-hash/181135/5 "2010-01-07T00:42:04Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 6, 2010, 11:07pm UTC](https://www.ruby-forum.com/t/whats-string-hash/181135/6 "2010-01-06T23:07:49Z")

</div>

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
```
