I want to use String.hash
to generate the hash code, but I'm worried that if some time later I upgrade the version from 1.8 to 1.9, the hash code generated will also change.
Do Ruby 1.8 and 1.9 have the same hash code for a string?
I want to use String.hash
to generate the hash code, but I'm worried that if some time later I upgrade the version from 1.8 to 1.9, the hash code generated will also change.
Do Ruby 1.8 and 1.9 have the same hash code for a string?
Fortunately, the answer is easy because they do not:
If you use the builtin hash method, I would recommend having a script as part of your build process that generates the necessary hashcodes. Note that they are not guaranteed to be the same even from one machine to the next.
If you need consistent hashing, use something like CRC32 or SHA1:
They have quite different purposes, but CRC32 has the advantage of returning a 32-bit number and being quite fast, while SHA1 is an 80-bit number but more secure. (I’m assuming this is not for cryptographic purposes, but look into SHA-256 if you need it.)