sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Mad behaviour of String#hash

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 4 Posters 484 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AdamBA Offline
      AdamB
      last edited by

      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".hash would 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

      Developer of LightUp Click for website

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        @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

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • TIGT Online
          TIG Moderator
          last edited by

          Or for an explanation see this http://stackoverflow.com/questions/6783811/why-is-ruby-string-hash-inconsistent-across-machines

          TIG

          1 Reply Last reply Reply Quote 0
          • tt_suT Offline
            tt_su
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @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

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                @tt_su said:

                This .hash change affects only String, yes?

                The above Ruby bugs thread mentions Object#hash being 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 Module class inherits from Object.

                (2) Close this SketchUp session, reopen SketchUp, and in the console:
                Sketchup.hash %(#004000)[>> -226388390]

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement