ruby的String .hash方法如何工作? [英] How does ruby's String .hash method work?

查看:110
本文介绍了ruby的String .hash方法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是红宝石的新手.我看过一个字符串方法(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 that a.eql?(b) implies a.hash == b.hash. The hash value is used by class Hash. Any hash value that exceeds the capacity of a Fixnum 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆