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表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...
随机推荐
- openwrt的路由器重置root密码
家里路由器刷了openwrt,结果长期没登录,忘了root密码. 很容易就找到了这里介绍的办法 http://www.openwrt.org.cn/bbs/thread-12327-1-1.html ...
- Windows Azure 名词定义(Glossary)
Glossary(名词) Definition(定义) Availability Set 可用性组 refers to two or more Virtual Machines deployed ac ...
- loaded the "XXXView" nib but the view outlet was not set 解决方案
'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "XXXView" nib but the view o ...
- 利用NABC模型进行竞争性需求分析
利用NABC模型进行竞争性需求分析: 1>N(Need 需求) 越来越多的,各式各样的游戏层出不穷,大的小的中等的都已经琳琅满目了,用户貌似都看不过眼了.现在大游戏费时伤神,当然了得在有 ...
- 解决vs2015使用fopen、fprintf等函数报错的问题
出现错误提示: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable ...
- ActiveRecord中andFilterWhere使用
查询数据库时 $model; if(!empty($name)) { $model->andWhere(['name'=>$name]); } 可以用andFilterWhere,自动的把 ...
- POJ2288 Islands and Bridges
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- hihocoder 1154 Spring Outing
传送门 #1154 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring ...
- Unix 目录结构的来历
Unix(包含Linux)的初学者,常常会很困惑,不明白目录结构的含义何在.Unix 目录结构的来历举例来说,根目录下面有一个子目录/bin,用于存放二进制程序.但是,/usr子目录下面还有/usr/ ...
- linux下的三种解压文件的命令?
那要看你的压缩文件使用哪种压缩方式:gzip,压缩文件名:zip或gz,解压命令:unzipbzip2,压缩文件名:bz,解压命令:bzip2 -d上面两个是最常用的压缩方式,一般在linux下可以通 ...