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. Asp.net MVC在View里动态捆绑压缩引用的js

    前言 Asp.net MVC 4以上版本多了BundleConfig.RegisterBundles方法,可以把要捆绑的脚本或样式进行捆绑压缩,以减少客户端的请求次数从而提高了客户端的访问速度. 问题 ...

  2. WCF入门(22)

    前言 本还想写一集WCF入门教程的,心情实在不好,明天又还有面试,改天再写吧. 说一下今天遇到的入职坑.面试能坑,上班能坑,完全没想到入职也能坑.切身经历. 今年10月份想换工作,更新了一下简历,接到 ...

  3. c#批量插入示例

    var sql = @"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20131029153010;I ...

  4. FilenameFilter用法

    使用FilenameFilter实现图片过滤,只要.gif,.jpg,.png文件. java 代码 public class ImageFilter implements FilenameFilte ...

  5. python 条件判断和循环

    一.条件判断    if if  age>= 18: 记住在判断语句后面要加上     : 还有要注意他的缩进 age = 20if age >= 18: print 'your age ...

  6. iOS边练边学--cocoaPods管理第三方框架--命令行方式实现

    更换源 Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.org/)源来查找.安装.升级和写在软件包 gem sources --remove ht ...

  7. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...

  8. Spring的辅助类

    http://www.cnblogs.com/maoan/p/3446224.html spring获取ApplicationContext对象的方法——ApplicationContextAware

  9. 【bzoj2463】 谁能赢呢?

    www.lydsy.com/JudgeOnline/problem.php?id=2463 (题目链接) 题意 一个n*n的棋盘,开始时左上角有一个棋子,每次可以把棋子向4个方向移动,但不能移动到曾经 ...

  10. poj3207 2-SAT入门

    一开始题意没读懂 = = 题意:比如说对于表盘上a到b.c到d都要连边,这两个边不能交叉.这两个边要么都在圆内要么都在圆外,而且可以是曲线= = 比如这种情况:(Reference:http://bl ...