为什么在 Ruby 中使用符号作为哈希键? [英] Why use symbols as hash keys in Ruby?

查看:31
本文介绍了为什么在 Ruby 中使用符号作为哈希键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多时候人们使用符号作为 Ruby 哈希中的键.

A lot of times people use symbols as keys in a Ruby hash.

与使用字符串相比有什么优势?

What's the advantage over using a string?

例如:

hash[:name]

对比

hash['name']

推荐答案

TL;DR:

使用符号不仅可以在进行比较时节省时间,还可以节省内存,因为它们只存储一次.

Ruby 符号是不可变的(无法更改),这使得查找内容变得更加容易

Ruby Symbols are immutable (can't be changed), which makes looking something up much easier

简短的回答:

使用符号不仅可以在进行比较时节省时间,还可以节省内存,因为它们只存储一次.

Ruby 中的符号基本上是不可变字符串" ..这意味着它们不能改变,这意味着在整个过程中多次引用相同的符号您的源代码始终存储为同一实体,例如具有相同的对象 ID.

Symbols in Ruby are basically "immutable strings" .. that means that they can not be changed, and it implies that the same symbol when referenced many times throughout your source code, is always stored as the same entity, e.g. has the same object id.

另一方面,字符串是可变的,它们可以随时更改.这意味着 Ruby 需要将您在整个源代码中提到的每个字符串存储在单独的实体中,例如如果你有一个字符串name"在您的源代码中多次提到,Ruby 需要将所有这些都存储在单独的 String 对象中,因为它们以后可能会发生变化(这是 Ruby 字符串的性质).

Strings on the other hand are mutable, they can be changed anytime. This implies that Ruby needs to store each string you mention throughout your source code in it's separate entity, e.g. if you have a string "name" multiple times mentioned in your source code, Ruby needs to store these all in separate String objects, because they might change later on (that's the nature of a Ruby string).

如果您使用字符串作为哈希键,Ruby 需要评估字符串并查看其内容(并在其上计算哈希函数)并将结果与​​已存储的键的(哈希)值进行比较在哈希中.

If you use a string as a Hash key, Ruby needs to evaluate the string and look at it's contents (and compute a hash function on that) and compare the result against the (hashed) values of the keys which are already stored in the Hash.

如果你使用一个符号作为哈希键,它隐含着它是不可变的,所以 Ruby 基本上可以将对象 ID 的(哈希函数)与键的(哈希)对象 ID 进行比较.已经存储在哈希中.(快得多)

If you use a symbol as a Hash key, it's implicit that it's immutable, so Ruby can basically just do a comparison of the (hash function of the) object-id against the (hashed) object-ids of keys which are already stored in the Hash. (much faster)

缺点:每个符号占用 Ruby 解释器符号表中的一个插槽,该插槽永远不会被释放.符号永远不会被垃圾收集.因此,当您有大量符号(例如自动生成的符号)时,会出现极端情况.在这种情况下,您应该评估这如何影响您的 Ruby 解释器的大小.

Downside: Each symbol consumes a slot in the Ruby interpreter's symbol-table, which is never released. Symbols are never garbage-collected. So a corner-case is when you have a large number of symbols (e.g. auto-generated ones). In that case you should evaluate how this affects the size of your Ruby interpreter.

注意事项:

如果您进行字符串比较,Ruby 可以通过比较它们的对象 ID 来比较符号,而无需评估它们.这比比较需要评估的字符串快得多.

If you do string comparisons, Ruby can compare symbols just by comparing their object ids, without having to evaluate them. That's much faster than comparing strings, which need to be evaluated.

如果您访问散列,Ruby 总是应用散列函数来计算散列键";从您使用的任何键.你可以想象像 MD5 哈希这样的东西.然后 Ruby 比较那些散列键".互相反对.

If you access a hash, Ruby always applies a hash-function to compute a "hash-key" from whatever key you use. You can imagine something like an MD5-hash. And then Ruby compares those "hashed keys" against each other.

每次在代码中使用字符串时,都会创建一个新实例 - 创建字符串比引用符号慢.

Every time you use a string in your code, a new instance is created - string creation is slower than referencing a symbol.

从 Ruby 2.1 开始,当您使用冻结字符串时,Ruby 将使用相同的字符串对象.这避免了必须创建相同字符串的新副本,并将它们存储在垃圾收集的空间中.

Starting with Ruby 2.1, when you use frozen strings, Ruby will use the same string object. This avoids having to create new copies of the same string, and they are stored in a space that is garbage collected.

长答案:

https://web.archive.org/web/20180709094450/http://www.reactive.io/tips/2009/01/11/the-difference-between-ruby-符号和字符串

http://www.randomhacks.net.s3-website-us-east-1.amazonaws.com/2007/01/20/13-ways-of-looking-at-a-ruby-symbol/

https://www.rubyguides.com/2016/01/ruby-可变性/

这篇关于为什么在 Ruby 中使用符号作为哈希键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆