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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.