Purpose of "text.hash"

D

Doug Hogg

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
 
G

Gareth Howells

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

Gareth
 
J

Jesús Gabriel y Galán

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?

http://en.wikipedia.org/wiki/Hash_function

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

Jesus.
 
R

R.F. Pels

Doug said:
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.
 

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

Members online

Forum statistics

Threads
474,271
Messages
2,571,357
Members
48,042
Latest member
DoraMcBrie

Latest Threads

Top