# Purpose of "text.hash"

**URL:** <https://www.ruby-forum.com/t/purpose-of-text-hash/121482>\
**Category:** Ruby\
**Created:** [November 28, 2007, 11:42am UTC](https://www.ruby-forum.com/t/purpose-of-text-hash/121482 "2007-11-28T11:42:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![douha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@douha](https://www.ruby-forum.com/u/douha)\
**Post date:** [November 28, 2007, 11:42am UTC](https://www.ruby-forum.com/t/purpose-of-text-hash/121482/1 "2007-11-28T11:42:25Z")

</div>

Hi,

I am reading “Beginning Ruby” by Peter C…

One of the methods listed on page 50 for operating on strings is  
“Test”.hash which returns -98625764 in the example listed.

I am trying to see how this would be used. Can anyone suggest an  
application for this method?

Thanks in advance,

Doug

---

<div class="post-metadata">

**Author:** ![douha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@douha](https://www.ruby-forum.com/u/douha)\
**Post date:** [November 28, 2007, 11:48am UTC](https://www.ruby-forum.com/t/purpose-of-text-hash/121482/2 "2007-11-28T11:48:15Z")

</div>

A very simple form of encryption perhaps? There are many situations  
where  
you would not want to leave a String as plaintext.

Gareth

---

<div class="post-metadata">

**Author:** ![douha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@douha](https://www.ruby-forum.com/u/douha)\
**Post date:** [November 28, 2007, 11:55am UTC](https://www.ruby-forum.com/t/purpose-of-text-hash/121482/3 "2007-11-28T11:55:20Z")

</div>

On Nov 28, 2007 11:42 AM, Doug H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I am reading “Beginning Ruby” by Peter C…
> 
> One of the methods listed on page 50 for operating on strings is  
> “Test”.hash which returns -98625764 in the example listed.
> 
> I am trying to see how this would be used. Can anyone suggest an  
> application for this method?

> **[Hash function](https://en.wikipedia.org/wiki/Hash_function)**
>
> A hash function is any function that can be used to map data of arbitrary size to fixed-size values, though there are some hash functions that support variable length output. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes. The values are usually used to index a fixed-size table called a hash table. Use of a hash function to index a hash table is called hashing or scatter storage addressing.
> Hash functions and their associated hash tables are...

It’s most usual application is in using the object as a key in a Hash.

Jesus.

---

<div class="post-metadata">

**Author:** ![douha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@douha](https://www.ruby-forum.com/u/douha)\
**Post date:** [November 28, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/purpose-of-text-hash/121482/4 "2007-11-28T23:37:47Z")

</div>

Doug H. wrote:

> One of the methods listed on page 50 for operating on strings is  
> “Test”.hash which returns -98625764 in the example listed.
> 
> I am trying to see how this would be used. Can anyone suggest an  
> application for this method?

Generating a hash from a string serves the purpose of converting this  
string  
into a more or less unique number. A full string can be quite long, and  
using it as a key to retrieve other information can make searching for  
that  
key time consuming. Comparing numbers - and especially integers - is  
much  
faster.

What usually happens is that both the hash and the string that serves as  
the  
ultimate key are stored with the data. This combination is stored in the  
order of the hash in a container. Access by hash from the container is  
very  
fast.

To retrieve the original data, use the string key, calculate the hash  
from  
the string key, use that hash to find all entries in the container with  
that hash, then finally using string compares to search for the  
definitive  
entry.
