How does ruby's String .hash method work?
The hash
method is defined for all objects. See documentation:
Generates a
Fixnum
hash value for this object. This function must have the property thata.eql?(b)
impliesa.hash == b.hash
. The hash value is used by classHash
. Any hash value that exceeds the capacity of aFixnum
will be truncated before being used.
So the String.hash
method is defined in C-Code. Basically (over-simplified) it just sums up the characters in that string.