简体   繁体   English

Ruby和:symbols

[英]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 ,则只会使用一个。

From Symbol doc: Symbol doc:

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 various to_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 if Fred 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: 将符号视为:

  • A method name that you plan to use later 您计划以后使用的方法名称
  • A constant / enumeration that you want to store and compare against 要存储和比较的常量/枚举

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 它们被用作

  • keys to arrays, so you look up :banana you only have one entry. 数组的键,因此您查找:banana时只有一项。 In most languages if these are Strings you run the risk of having multiple Strings in memory with the text "banana" and not having the code detect they are the same 在大多数语言中,如果这些是字符串,则冒着在内存中使用文本“ banana”存储多个字符串并且没有代码检测到它们相同的风险。
  • method/proc names. 方法/过程名称。 Most people are familiar with how C distinguishes a method from its call with parentheses: my_method vs. my_method(). 大多数人都熟悉C如何用括号将方法与调用区别开来:my_method与my_method()。 In Ruby, since parentheses are optional, these both indicate a call to that method. 在Ruby中,由于括号是可选的,所以两者都表示对该方法的调用。 The symbol, however, is convenient to use as a standin for methods (even though there really is no relationship between a symbol and a method). 但是,符号可以方便地用作方法的替代(即使符号和方法之间确实没有关系)。
  • enums (and other constants). 枚举(和其他常量)。 Since they don't change they exhibit many of the properties of these features from other languages. 由于它们没有改变,因此具有其他语言所提供的这些功能的许多特性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM