why we use Symbols in Hash

时间:2022-09-03 13:12:00

Rather than using Strings as the keys in a Hash, it’s better practice to use Symbols.

Symbols are just like Strings except they’re faster and take up less memory. The reason Symbols are so efficient is that they are immutable; they cannot be changed, so Ruby doesn’t have to allocate as much memory. Strings on the other hand, can be changed, so Ruby allocates more memory to allow for that. If you want a more thorough explanation of why they are faster, check out thisblog post.

Let’s try using them as keys in a Hash. Here’s a version of a Hash that uses Strings as keys:

kitten = {
"name" => "Blue Steele",
"breed" => "Scottish Fold",
"age" => "13 weeks"
}

We can rewrite it using Symbols:

kitten = {
:name => "Blue Steele",
:breed => "Scottish Fold",
:age => "13 weeks"
}

Aside from the slight performance boost, another good reason to use Symbols is that Ruby 1.9 introduced a “shortcut” syntax for declaring Hashes with Symbols as keys. We could rewrite the above Hash as:

kitten = {
name: "Blue Steele",
breed: "Scottish Fold",
age: "13 weeks"
}

It saves us having to type those annoying hash rockets (=>), and it closely models the syntax of other languages like JavaScript.

To wrap up, it’s a good idea to use Symbols as keys in a Hash because they’re slightly faster than Strings, and Ruby provides us a nice shortcut syntax.

why we use Symbols in Hash的更多相关文章

  1. How to pronounce symbols on keyboard

    Refefrence: http://answers.yahoo.com/question/index?qid=20100607151104AAtQxhc ~ “tilde” or “tweedle” ...

  2. How to say all the keyboard symbols in English and Chinese

    How to say all the keyboard symbols in English Symbol English 中文 ~ tilde 波浪号 ` grave accent, backquo ...

  3. (转)How Hash Algorithms Work

    本文转自:http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software   Home Posted: Novembe ...

  4. [20191127]表 full Hash Value的计算.txt

    [20191127]表 full Hash Value的计算.txt --//曾经做过表full Hash Value的计算,当时我是通过建立简单的schema以及表名的形式,使用hashcat破解o ...

  5. Extremely fast hash algorithm-xxHash

    xxHash - Extremely fast hash algorithm xxHash is an Extremely fast Hash algorithm, running at RAM sp ...

  6. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  7. 对抗密码破解 —— Web 前端慢 Hash

    (更新:https://www.cnblogs.com/index-html/p/frontend_kdf.html ) 0x00 前言 天下武功,唯快不破.但在密码学中则不同.算法越快,越容易破. ...

  8. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  9. hash表长度优化证明

    hash表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...

随机推荐

  1. (原创)解决.net 下使用uploadify,在火狐浏览器下的error 302

    简单粗劣说下哈,通过uploadify中flash在火狐下上传,造成了erroe 302, 是因为其session丢失,并修改了其sessionID. 网上有很多案列,可并没有这么直接.感觉绕了点弯. ...

  2. s1=s1+1与s1+=1的区别

    刚看到一面试题,题目是这样的:short s1=1;s1=s1+1;有什么错?short s1=1;s1+=1;有什么错? 初看之下就是s1=s1+1和s1+=1的区别.在开发中我们基本上是使用后一种 ...

  3. node.js使用汇总贴

    金天:学习一个新东西,就要持有拥抱的心态,如果固守在自己先前的概念体系,就会有举步维艰的感觉..NET程序员初用node.js最需要适应的就是异步开发,以及弱类型语言难以避免的拼写错误与弱小的语法提示 ...

  4. 教你修改Linux下高并发socket最大连接数所受的各种限制

    1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开 文件数量的限制(这是因为系统为 ...

  5. mockServer学习

    mockServer学习 很喜欢mockserver官方主页的背景颜色和格式 官方主页如下: http://www.mock-server.com/

  6. Naive Bayes Theorem and Application - Theorem

    Naive Bayes Theorm And Application - Theorem Naive Bayes model: 1. Naive Bayes model 2. model: discr ...

  7. LeetCode 190. Reverse Bits (反转位)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  8. Win10问题汇总

    1.重置网络连接命令 netsh winsock reset ipconfig /flushdns 2.WIN10去除我的电脑上面的6个文件夹 把下面代码复制,保存到.reg中,然后执行即可(修改注册 ...

  9. DataNitro安装配置

    必须安装python2,并配置环境变量,第一次安装时没有安装python,第一次打开可以使用,后来就打不开了,卸载重装也不行,安装Python2之后才可以. 按步骤安装就可以 不支持WPS

  10. 16 多校8 Rikka with Parenthesis II

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...