Purpose of "text.hash"

Hi,

I am reading "Beginning Ruby" by Peter Cooper.

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

···

--
Posted via http://www.ruby-forum.com/.

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

Gareth

···

Hi,

I am reading "Beginning Ruby" by Peter Cooper.

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
--
Posted via http://www.ruby-forum.com/\.

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

Jesus.

···

On Nov 28, 2007 11:42 AM, Doug Hogg <dough@machelpla.com> wrote:

Hi,

I am reading "Beginning Ruby" by Peter Cooper.

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?

Doug Hogg 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.

···

--
Ruurd