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. SequoiaDB 系列之一 :SequoiaDB的安装、部署

    在分析或者参与一个开源项目之前,了解项目构建的目的是有必要的. 既然SequoiaDB是NoSQL数据库产品,则必然存在于传统关系型数据库相同的功能点:数据的增.删.改和查询(CRUD). 先了解怎么 ...

  2. 第四十四课:jQuery UI和jQuery easy UI

    jQuery UI是jQuery官方提供的功能效果和UI样式.作为官方出的东西,它一直没有被人们看重,一是它没有datagrid,tree等UI库必备的东西,二是它修改太过频繁,体积庞大.其实它所有以 ...

  3. 一头扎进EasyUI2

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 一头扎进EasyUI第6讲 .日历组件 <div class="easyui-calendar&quo ...

  4. 第五次课堂总结x

    一.知识点: 1.while语句 循环体语句:           while语句里的表达式可以是任何合法的表达式,循环体则只可以表达一条语句. while的循环体语句需要能改变循环条件的真假条件. ...

  5. datepicker自定义 -- iOS

    /** * 创建时间选择器 */ - (void)createPickerView { self.datePicker = [[UIDatePicker alloc] init]; _datePick ...

  6. Jetty+json-lib库抛异常的问题解决过程(java.lang.NoClassDefFoundError: net/sf/json/JSONObject)

      一.之前抛异常是将json库改成了fastjson解决的,参见: http://www.cnblogs.com/gossip/p/5369670.html     异常信息:     二.解决步骤 ...

  7. Hibernate-Criteria用法

    criteria 英[kraɪˈtɪərɪə] 美[kraɪˈtɪrɪə] Hibernate 设计了 CriteriaSpecification Hibernate 设计了 CriteriaSpec ...

  8. Spring-涉及到的设计模式汇总

    1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类. ...

  9. 一个项目中哪些文件是要上传到 git上的,哪些是不必要的

  10. 最短路之Dijkstra算法

    1. 邻接矩阵 int cost[MAX_V][MAX_V]; //assume cost[u][v]>0 int d[MAX_V]; bool used[MAX_V]; void Dijkst ...