I'm just a newbie to ruby. I've seen a string method (String).hash .
For example, in irb
, I've tried
>> "mgpyone".hash
returns
=> 144611910
how does this method works ?
I'm just a newbie to ruby. I've seen a string method (String).hash .
For example, in irb
, I've tried
>> "mgpyone".hash
returns
=> 144611910
how does this method works ?
If you need to get a consistent hashing output I would recommend NOT to use
'string.hash
but instead consider using Digest::MD5 which will be safe in multi-instance cloud applications for example you can test this as mentioned in comment in previous by @BenCrowellRun this 2x from your terminal, you will get different output each time:
But if you run this the output will be consistent:
The
hash
method is defined for all objects. See documentation:So the
String.hash
method is defined in C-Code. Basically (over-simplified) it just sums up the characters in that string.