Ruby 中的符号表与其他语言中的符号表有什么不同
符号表上的维基百科条目是一个很好的参考:
http://en.wikipedia.org/wiki/Symbol_table
但是当我尝试理解 Ruby 中的符号以及它们如何在符号数组(由 Symbol.all_symbols
方法返回)中表示时,
我想知道 Ruby 处理符号的方法是否正确table 与其他语言有什么重要的区别吗?
The wikipedia entry on Symbol tables is a good reference:
http://en.wikipedia.org/wiki/Symbol_table
But as I try to understand symbols in Ruby and how they are represented in the Array of Symbols (returned by the Symbol.all_symbols
method),
I'm wondering whether Ruby's approach to the symbol table has any important differences from other languages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从这个意义上来说,Ruby 并没有真正的“符号表”。 它有绑定和符号(lispers 称之为原子),但它不是并没有真正按照文章描述的方式进行操作。
因此,回答你的问题:红宝石并不是以不同的方式完成相同的事情,而是它做了两件不同的事情(
:xxx
符号 --> 中的唯一 ID 和绑定范围)并为它们使用相似/重叠的术语。澄清一下:
您链接到的文章给出了符号表的常规定义,即
但这不是 ruby 符号表的作用。 它只是为某类对象提供全局唯一的标识,在源代码中可以写成
:something
,包括:+
和:"嗨,鲍勃!”
这不是标识符。 此外,仅使用标识符不会创建相应的符号。 最后,上面段落中列出的任何信息都没有存储在 ruby 的符号列表中。这是命名上的巧合,阅读那篇文章不会帮助您理解 ruby 的符号。
Ruby doesn't really have a "symbol table" in that sense. It has bindings, and symbols (what lispers call atoms) but it isn't really doing it the way that article describes.
So in answer to your question: it isn't so much that ruby has the same thing done differently, but rather that it does two different things (
:xxx
notation --> unique ids and bindings in scopes) and uses similar / overlapping terminology for them.To clarify:
The article you link to gives the conventional definition of a symbol table, to wit
But this isn't what ruby's symbol table does. It just provides a globally unique identity for a certain class of objects which can be written as
:something
in the source code, including things like:+
and:"Hi bob!"
which aren't identifiers. Also, merely using an identifier will not create a corresponding symbol. And finally, none of the information listed in the passage above is stored in ruby's list of symbols.It's a coincidence of naming, and reading that article will not help you understand ruby's symbols.
最大的区别是(像 Lisp 一样)Ruby 实际上有符号语法,并且您可以自己在运行时轻松添加/删除内容。 如果你说:balloon(或“balloon”.intern),它会为你实习。 即使您在源代码中通过名称引用它,但在内部它只是符号表中的一个指针。 如果比较符号,则只是指针比较,而不是字符串比较。
像 C 这样的语言实际上没有办法在运行时简单地说“为我创建一个新符号”。 您可以通过定义函数在编译时隐式地执行此操作,但这实际上是它的唯一用途。 由于 C 没有符号语法,因此如果您希望能够在程序中说出 Balloon 但能够将其与单个机器指令进行比较,则可以使用枚举(或#defines)。
在 Ruby 中,只需一个字符即可构成一个符号,因此您可以将它用于各种用途(例如哈希键)。
The biggest difference is that (like Lisp) Ruby actually has a syntax for symbols, and it's easy to add/remove things at runtime yourself. If you say :balloon (or "balloon".intern) it will intern that for you. Even though you're referring to it by name in your source, internally it's just a pointer in the symbol table. If you compare symbols, it's just a pointer-compare, not a string-compare.
Languages like C don't really have a way to say simply "create a new symbol for me" at runtime. You can do it implicitly at compile-time by defining a function, but that's really its only use. Since C has no syntax for symbols, if you want to be able to say Balloon in your program but be able to compare it with a single machine instruction, you use enums (or #defines).
In Ruby, it takes only one character to make a symbol, so you can use it for all kinds of things (like hash keys).
Ruby 中的符号用于其他语言倾向于使用枚举、定义、常量等的地方。 它们也经常用于关联键。 它们的使用与该文章中讨论的符号表关系不大,只是它们显然存在于符号表中。
Symbols in Ruby are used where other languages tend to use enums, defines, constants and the like. They're also often used for associative keys. Their use has little to do with a symbol table as discussed in that article, except that they obviously exist in one.