[英]Ruby and :symbols
I have just started using Ruby and I am reading "Programming Ruby 1.9 - The Pragmatic Programmer's Guide". 我刚开始使用Ruby,正在阅读“编程Ruby 1.9-实用程序员指南”。 I came across something called symbols, but as a PHP developer I don't understand what they do and what they are good for. 我碰到了一种叫做符号的东西,但是作为PHP开发人员,我不了解它们的作用和优点。
Can anyone help me out with this? 任何人都可以帮我解决这个问题吗?
It's useful to think of symbols in terms of "the thing called." 用“事物”来思考符号是很有用的。 In other words, :banana is referring to "the thing called banana." 换句话说,:banana指的是“香蕉”。 They're used extensively in Ruby, mostly as Hash (associative array) keys. 它们在Ruby中被广泛使用,主要用作Hash(关联数组)键。
They really are similar to strings, but behind the scenes, very different. 它们确实类似于弦乐,但在幕后却大不相同。 One key difference is that only one of a particular symbol exists in memory. 一个关键的区别是内存中仅存在一个特定符号中的一个。 So if you refer to :banana 10 times in your code, only one instance of :banana is created and they all refer to that one. 因此,如果您在代码中多次引用:banana,则只会创建:banana的一个实例,并且它们都引用该实例。 This also implies they're immutable. 这也意味着它们是不可变的。
Symbols are similar to string literals in the sense that share the same memory space, but it is important to remark they are not string equivalents. 符号在共享相同存储空间的意义上类似于字符串文字,但是重要的是要注意它们不是字符串等效项。
In Ruby, when you type "this"
and "this"
you're using two different memory locations; 在Ruby中,当您键入"this"
和"this"
您使用的是两个不同的内存位置。 by using symbols you'll use only one name during the program execution. 通过使用符号,您将在程序执行期间仅使用一个名称。 So if you type :this
in several places in your program, you'll be using only one. 因此,如果在程序的多个位置键入:this
,则只会使用一个。
Symbol objects represent names and some strings inside the Ruby interpreter. 符号对象表示Ruby解释器中的名称和一些字符串。 They are generated using the
:name
and:"string"
literals syntax, and by the variousto_sym
methods. 它们是使用:name
和:"string"
文字语法以及各种to_sym
方法生成的。 The same Symbol object will be created for a given name or string for the duration of a program's execution, regardless of the context or meaning of that name. 在程序执行期间,将为给定的名称或字符串创建相同的Symbol对象,而不管该名称的上下文或含义如何。 Thus ifFred
is a constant in one context, a method in another, and a class in a third, the Symbol:Fred
will be the same object in all three contexts. 因此,如果Fred
在一个上下文中是一个常量,在另一个上下文中是一个方法,而在第三个上下文中是一个类,则Symbol:Fred
将在所有三个上下文中都是相同的对象。
So, you basically use it where you want to treat a string as a constant. 因此,您基本上可以在要将字符串视为常量的地方使用它。
For instance, it is very common to use it with the attr_accessor
method, to define getter/setter for an attribute. 例如,将它与attr_accessor
方法一起使用以定义属性的getter / setter是很常见的。
class Person
attr_accessor :name
end
p = Person.new
p.name= "Oscar"
But this would do the same: 但这会做同样的事情:
class DontDoThis
attr_accessor( "name" )
end
ddt = DontDoThis.new
ddt.name= "Dont do it"
Think of a Symbol as a either: 将符号视为:
For example: 例如:
s = "FooBar"
length = s.send(:length)
>>> 6
@AboutRuby has a good answer, using the terms "the thing called". @AboutRuby使用术语“被称为的东西”有一个很好的答案。
:banana is referring to "the thing called banana." :banana指的是“香蕉”。
He notes that you can refer to :banana many times in the code and its the same object-- even in different scopes or off in some weird library. 他指出,您可以在代码及其同一对象中多次引用:banana,甚至在不同范围内或在某些奇怪的库中也可以引用。 :banana is the thing called banana, whatever that might mean when you use it. :banana是所谓的香蕉,无论您使用它意味着什么。
They are used as 它们被用作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.