why we use Symbols in Hash
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的更多相关文章
- How to pronounce symbols on keyboard
Refefrence: http://answers.yahoo.com/question/index?qid=20100607151104AAtQxhc ~ “tilde” or “tweedle” ...
- 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 ...
- (转)How Hash Algorithms Work
本文转自:http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software Home Posted: Novembe ...
- [20191127]表 full Hash Value的计算.txt
[20191127]表 full Hash Value的计算.txt --//曾经做过表full Hash Value的计算,当时我是通过建立简单的schema以及表名的形式,使用hashcat破解o ...
- Extremely fast hash algorithm-xxHash
xxHash - Extremely fast hash algorithm xxHash is an Extremely fast Hash algorithm, running at RAM sp ...
- 复杂的 Hash 函数组合有意义吗?
很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...
- 对抗密码破解 —— Web 前端慢 Hash
(更新:https://www.cnblogs.com/index-html/p/frontend_kdf.html ) 0x00 前言 天下武功,唯快不破.但在密码学中则不同.算法越快,越容易破. ...
- 散列表(hash table)——算法导论(13)
1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...
- hash表长度优化证明
hash表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...
随机推荐
- VMware打卡虚拟机提示“此虚拟机可能已被复制或移动”
使用VMware打开虚拟机时出现下图的页面,我来解释一下这三个选项按钮的区别与作用. "我已移动虚拟机" //表示打开后的虚拟的网卡的mac地址不变,如果复制本地的,同时开 ...
- (旧)子数涵数·PS ——翻页效果
一.首先在网络上下载一张图片,作为素材.这是我下载的素材,至于为什么选择这张照片呢,当然不是因为自己的一些羞羞的念头啦. 二.打开Photoshop,我使用的版本是CS3(因为CS3所占的磁盘空间较小 ...
- Qt模型/视图框架----简单的例子
#include<qapplication.h> #include<qfilesystemmodel.h> #include<qtreeview.h> #inclu ...
- 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用
原文地址http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax- ...
- JS建造者模式
function getBeerById( id, callback){ _request('GET','URL'+id,function(res){ callback(res.responseTex ...
- Java 使用正则表达式
用正则表达式来处理掉内容中的特定字符,下面的代码为,去掉P标签中的属性width 设置.将P标签处理后在拼接成字符串 /** * 给 P 标签去掉width 样式设置 * @param content ...
- Spring 事物机制
Spring两种事物处理机制,一是声明式事物,二是编程式事物 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的. 其本质是对方法前后进行拦截,然后在目标方法开始之前创建或 ...
- Java编程思想学习(十四) 枚举
关键字enum可以将一组具名的值有限集合创建一种为新的类型,而这些具名的值可以作为常规的程序组件使用. 基本enum特性 调用enum的values()方法可以遍历enum实例,values()方法返 ...
- BZOJ-1625 宝石手镯 01背包(傻逼题)
傻逼题,懒得打,复制蛋蛋的.. 1625: [Usaco2007 Dec]宝石手镯 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1076 Solved: ...
- 【uoj150】 NOIP2015—运输计划
http://uoj.ac/problem/150 (题目链接) 题意 给出一棵树以及m个询问,可以将树上一条边的权值修改为0,求经过这样的修改之后最长的边最短是多少. Solution 老早就听说过 ...