Ruby 中的符号
不知道我是否明白什么是符号。我在不同的地方读到以下内容:
- 符号是不可变的,这意味着内容在初始化后不会改变。这意味着该对象可以有许多对其的引用,因为内容无法更改。
- 在内容重要的地方使用符号来表示身份和字符串。
- 符号对象仅实例化一次,并且只要进程运行就存在。
- 关于符号表
我不明白这些短语。最后,我不明白符号对象是什么。 :name
是一个符号对象,还是 :name
只是对其他地方的符号对象的引用?
I don't know if I understand what a symbol is. I read the following in different places:
- Symbols are immutable, meaning that the content won't change after they are initialized. This means that the object can have many references to it since the content can't change.
- Use Symbols for identity and strings where content matters.
- Symbol object are only instantiated once, and exists as long as the process runs.
- About symbol table
I do not understand these phrases. Last, I don't understand what a symbol object is. Is :name
a symbol object, or is :name
just a reference to a symbol object somewhere else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
'Eloquent Ruby' 书中的这句话帮助我理解了符号,有书中有一整章讨论字符串和符号,这也非常好。
This quote from the book 'Eloquent Ruby' helped me understand symbols, there's a whole chapter in the book dealing with strings and symbols that is also very good.
也许这个 IRB 会话可以让事情变得更清晰:
想象一下
object_id
方法类似于对象的地址。正如您所看到的,“First”字符串每次都有不同的地址(意味着每次都会创建一个新对象),而符号不会改变。也许您可以将符号与单例关联起来(如在单例模式中,而不是以元编程方式),以便于理解。Perhaps this IRB session can clear things up a little:
Imagine the
object_id
method as being similar to an object's address. As you can see, the "First" string has a different address each time ( meaning a new object gets created each time ), while the symbol doesn't change. Perhaps you could associate symbols with singletons ( as in the singleton pattern, not in a metaprogramming way ), for easier understanding.您可以将符号视为一种常量,但它不包含实际值。在没有符号的语言中,为了方便起见,您可以这样做:
在 Ruby 中,您可以使用符号来实现此目的:
它更有效,因为符号就是符号。它不存储值或任何内容,因此您不需要初始化它们。我不确定 Ruby 解释器中如何处理符号,但我认为这与任何新手 Ruby 程序员无关。
可以在这里找到一个很好的指南: http://www.troubleshooters.com/codecorn/ruby /符号.htm
You could regard symbols as a sort of constants, but that don't contain an actual value. In languages without symbols you might do something like this for convenience:
In Ruby you can use symbols for this:
It's more efficient since a symbol is just that, a symbol. It doesn't store a value or anything so you don't need to initialize them. I'm not sure how symbols are handled in the Ruby interpreter, but I don't think that's relevant for any novice ruby programmer.
A good guide can be found here: http://www.troubleshooters.com/codecorn/ruby/symbols.htm
我认为符号最好通过用例来解释。
对于创建常量,它们比整数更具表现力。
比条件语句中的比较更具可读性
,符号只是通过其唯一地址进行比较,因此它们比字符串更有效,字符串是按长度进行比较,然后是每个字符。
这比使用符号的效率要低。
I think symbols are best explained via use-cases.
For creating constants, they are more expressive than integers.
Is more readable than
For comparisons in conditional statements, symbols are just compared via their unique address, so they are more efficient than strings, which are compared by length and then every character.
This is going to be less efficient than using symbols.
这篇文章也可能令人感兴趣 http ://www.robertsosinski.com/2009/01/11/the-difference- Between-ruby-symbols-and-strings/因为它帮助我理解符号与字符串有何不同。
This article can be also of interest http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ as it helped me to understand how symbols differ from strings.
Symbol
实例是一组有序的字符。String
实例是字符的容器,可以包含任何字符序列的位置。A
Symbol
instance is an ordered set of characters. AString
instance is a container for characters, a place that can contain any sequence of characters.