[Nitro] Problems with String#hash
Dan North
dan at tastapod.com
Fri Nov 24 11:18:52 EST 2006
Hi George.
The contract with String.hash is that two objects that are equal will
hash to the same value. (Other objects can hash to the same value too,
but two equal objects can never have different hash values). Different
versions of Ruby - or the same version of Ruby on different systems - or
even different invocations of the same Ruby interpreter - are all free
to make up any values they like, as long as they do it consistently
/within an invocation/.
What that means is, you can guarantee that if you put something in a
hash bucket (say a Set or using as a key in a Hash) you will find it
when you look for it with another key with the same value.
What you /can't/ do is use a hash as, say, an externalized form for a
string (which wouldn't make sense anyway, because hashes are lossy by
design).
Thinking about it, this means you shouldn't use hashes in any kind of
DRb distributed cache or remoting, because the server's hash
representation is not guaranteed to be the same as the client's.
What are you trying to do with hashes that's causing you trouble?
Cheers,
Dan
George Moschovitis wrote:
> Dear devs,
>
> I am facing a very NASTY problem with String#hash
>
> Here is the same code that I run on 3 different computers:
>
> root at kosh:~ # irb
> irb(main):001:0> "User".hash
> => 690465448
> irb(main):002:0> VERSION
> => "1.8.2"
>
> C:\Documents and Settings\drak>irb
> irb(main):001:0> "User".hash
> => 690465448
> irb(main):002:0> VERSION
> => "1.8.4"
>
> http://tryruby.hobix.com/
> Interactive ruby ready.
>
>>> "User".hash
>>>
> => 690465448
>
>>> VERSION
>>>
> => "1.8"
>
> root at victory:~# irb
> irb(main):001:0> "User".hash
> => -1457018200
> irb(main):002:0> VERSION
> => "1.8.4"
>
> Please nottice that on the last case I get a different result! Any
> idea why this happens?
>
> thanks in advance for any help!
>
> George.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/nitro-general/attachments/20061124/7f52095e/attachment.html
More information about the Nitro-general
mailing list