ruby的String .hash方法如何工作? [英] How does ruby's String .hash method work?
问题描述
我只是红宝石的新手.我看过一个字符串方法(String).hash.
I'm just a newbie to ruby. I've seen a string method (String).hash .
例如,在irb
中,我尝试过
>> "mgpyone".hash
返回
=> 144611910
此方法如何工作?
推荐答案
为所有对象定义了hash
方法.请参见文档:
The hash
method is defined for all objects. See documentation:
为此生成一个
Fixnum
哈希值 目的.此功能必须具有a.eql?(b)
暗示a.hash == b.hash
的属性. 哈希值由类Hash
使用.任何散列值 超过Fixnum
的容量 在使用之前被截断.
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.
因此String.hash
方法是在 C-代码.基本上(过于简化)它只是将字符串中的字符求和.
So the String.hash
method is defined in C-Code. Basically (over-simplified) it just sums up the characters in that string.
这篇关于ruby的String .hash方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!