Mad behaviour of String#hash
-
I recently fixed a bug in LightUp that was the result of a subtle change in the method #hash between Ruby 1.8 and 2.0 that I thought I'd share.
Previously,
"adam".hashwould always give the same result - because its a hash of the string "adam" - which is what you'd expect.Ruby 2.0, chooses to give you a different result every time you launch Ruby. Which is quite idiotic.
The upshot is you cannot rely on creating digests that persist across sessions.

Sadly, there is no doubt a thread somewhere in Rubyland explaining why its a stroke of brilliance.. sigh
-
@adamb said:
Sadly, there is no doubt a thread somewhere in Rubyland explaining why its a stroke of brilliance.. sigh
Try:
https://bugs.ruby-lang.org/search?utf8=%E2%9C%93%26amp;q=String%23hash -
Or for an explanation see this http://stackoverflow.com/questions/6783811/why-is-ruby-string-hash-inconsistent-across-machines
-
Oh... I might have to have a scan of my extensions. I'm not sure if I store string hashes, but I wouldn't be surprised.
This .hash change affects only String, yes?Thanks for the heads up.
-
@tig said:
Or for an explanation see this http://stackoverflow.com/questions/6783811/why-is-ruby-string-hash-inconsistent-across-machines
Which actually quotes from the Ruby bugs dB at:
Bug #4103 (Closed): String#hash not returning consistent values in different sessions -
@tt_su said:
This
.hashchange affects only String, yes?The above Ruby bugs thread mentions
Object#hashbeing overhauled (which is inherited by every object, unless overriden.)So a test under SketchUp 2014 & Ruby 2.0:
(1) Open a SketchUp session, and in the console:
Sketchup.hash %(#004000)[>> -112701995]This is the method that the
Moduleclass inherits fromObject.(2) Close this SketchUp session, reopen SketchUp, and in the console:
Sketchup.hash %(#004000)[>> -226388390]
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement