Ruby 中的符号

发布于 2024-11-24 14:16:08 字数 287 浏览 1 评论 0原文

不知道我是否明白什么是符号。我在不同的地方读到以下内容:

  • 符号是不可变的,这意味着内容在初始化后不会改变。这意味着该对象可以有许多对其的引用,因为内容无法更改。
  • 在内容重要的地方使用符号来表示身份和字符串。
  • 符号对象仅实例化一次,并且只要进程运行就存在。
  • 关于符号表

我不明白这些短语。最后,我不明白符号对象是什么。 :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 :namejust a reference to a symbol object somewhere else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

把回忆走一遍 2024-12-01 14:16:08

'Eloquent Ruby' 书中的这句话帮助我理解了符号,有书中有一整章讨论字符串和符号,这也非常好。

答案是,我们倾向于在代码中使用字符串来表示两个不同的情况:
其他用途:字符串的第一个也是最明显的用途是保存一些数据
我们正在处理。从数据库中读入这些 Book 对象,你会非常
您的手中可能充满了字符串数据,例如书名、作者的信息
名称和实际文本。

我们使用字符串的第二种方式是在我们的专业中表示事物
克,比如想要查找表中的所有记录之类的事情。关键是关于
:在我们的 Book ActiveRecord 示例中,ActiveRecord 可以在以下情况下识别它:
看到它 - 代码需要知道要返回哪些记录,并且 :all 是表示的标志
它应该返回每一个。使用类似 :all for this 的好处是
某种“代表”责任是它对人类来说也有意义:你比
当你遇到 :all 时,你可能会比 0、-1 甚至是更容易理解它的含义
(天堂禁止!)0x29ef。

字符串的这两种用途 - 用于常规数据处理任务
一方面用于内部的、象征性的、标记类型的工作——使得非常不同
对对象的不同要求。如果您正在处理数据,您将希望拥有
触手可及的全系列字符串操作工具:您可能想要第一个
标题的十个字符,或者您可能想获取其长度或查看是否匹配
一些正则表达式。另一方面,如果您使用一些字符来站立
对于代码中的某些内容,您可能对弄乱不太感兴趣
实际角色。相反,在第二种情况下,您只需要知道这件事是否
是告诉您查找所有记录或仅查找第一条记录的标志。主要是当
你想要一些字符代表某种东西,你只需要知道这是否是
与此相同,快速可靠。

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.

The answer is that we tend to use strings of characters in our code for two rather dif-
ferent purposes: The first, and most obvious, use for strings is to hold some data that
we are processing. Read in those Book objects from the database and you will very
likely have your hands full of string data, things like the title of the book, the author’s
name, and the actual text.

The second way that we use strings of characters is to represent things in our pro-
grams, things like wanting to find all of the records in a table. The key thing about
:all in our Book ActiveRecord example is that ActiveRecord can recognize it when it
sees it—the code needs to know which records to return, and :all is the flag that says
it should return every one. The nice thing about using something like :all for this
kind of “stands for” duty is that it also makes sense to the humans: You are a lot more
likely to recognize what :all means when you come across it than 0, or -1, or even
(heaven forbid!) 0x29ef.

These two uses for strings of characters—for regular data processing tasks on the
one hand and for internal, symbolic, marker-type jobs on the other—make very dif-
ferent demands on the objects. If you are processing data, you will want to have the
whole range of string manipulation tools at your fingertips: You might want the first
ten characters of the title, or you might want to get its length or see whether it matches
some regular expression. On the other hand, if you are using some characters to stand
for something in your code, you probably are not very interested in messing with the
actual characters. Instead, in this second case you just need to know whether this thing
is the flag that tells you to find all the records or just the first record. Mainly, when
you want some characters to stand for something, you simply need to know if this is
the same as that, quickly and reliably.

掐死时间 2024-12-01 14:16:08

也许这个 IRB 会话可以让事情变得更清晰:

irb(main):001:0> "First".object_id
=> 23849064
irb(main):002:0> "First".object_id
=> 23842248
irb(main):003:0> "First".object_id
=> 23835432
irb(main):004:0> "First".object_id
=> 23828616
irb(main):005:0> :First.object_id
=> 301892
irb(main):006:0> :First.object_id
=> 301892
irb(main):007:0> :First.object_id
=> 301892
irb(main):008:0> :First.object_id
=> 301892
irb(main):009:0>

想象一下 object_id 方法类似于对象的地址。正如您所看到的,“First”字符串每次都有不同的地址(意味着每次都会创建一个新对象),而符号不会改变。也许您可以将符号与单例关联起来(如在单例模式中,而不是以元编程方式),以便于理解。

Perhaps this IRB session can clear things up a little:

irb(main):001:0> "First".object_id
=> 23849064
irb(main):002:0> "First".object_id
=> 23842248
irb(main):003:0> "First".object_id
=> 23835432
irb(main):004:0> "First".object_id
=> 23828616
irb(main):005:0> :First.object_id
=> 301892
irb(main):006:0> :First.object_id
=> 301892
irb(main):007:0> :First.object_id
=> 301892
irb(main):008:0> :First.object_id
=> 301892
irb(main):009:0>

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.

星星的轨迹 2024-12-01 14:16:08

您可以将符号视为一种常量,但它不包含实际值。在没有符号的语言中,为了方便起见,您可以这样做:

const BLUE = 1
const GREEN = 2

my_color = GREEN

...


if (my_color == GREEN)

在 Ruby 中,您可以使用符号来实现此目的:

my_color = :green

...

if my_color == :green

它更有效,因为符号就是符号。它不存储值或任何内容,因此您不需要初始化它们。我不确定 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:

const BLUE = 1
const GREEN = 2

my_color = GREEN

...


if (my_color == GREEN)

In Ruby you can use symbols for this:

my_color = :green

...

if my_color == :green

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

青春如此纠结 2024-12-01 14:16:08

我认为符号最好通过用例来解释。

对于创建常量,它们比整数更具表现力。

def rgb(color_symbol)
  return nil unless color_symbol.is_a? Symbol
  case color_symbol
  when :red;   { r: 255, g: 0, b: 0 }
  when :green; { r: 0, g: 255, b: 0 }
  when :blue;  { r: 0, g: 0, b: 255 }
  else; { r: 0, g: 0, b: 0 }
  end
end

p rgb(color_symbol= :red)

比条件语句中的比较更具可读性

def rgb(color_number)
  return nil unless color_number.is_a? Fixnum
  case color_number
  when 1; { r: 255, g: 0, b: 0 }
  when 2; { r: 0, g: 255, b: 0 }
  when 3; { r: 0, g: 0, b: 255 }
  else; { r: 0, g: 0, b: 0 }
  end
end

p rgb(color_number= 2)

,符号只是通过其唯一地址进行比较,因此它们比字符串更有效,字符串是按长度进行比较,然后是每个字符。

def rgb(color_string)
  return nil unless color_string.is_a? String
  case color_string
  when "red";   { r: 255, g: 0, b: 0 }
  when "green"; { r: 0, g: 255, b: 0 }
  when "blue";  { r: 0, g: 0, b: 255 }
  else; { r: 0, g: 0, b: 0 }
  end
end

p rgb(color_string= "blue")

这比使用符号的效率要低。

I think symbols are best explained via use-cases.

For creating constants, they are more expressive than integers.

def rgb(color_symbol)
  return nil unless color_symbol.is_a? Symbol
  case color_symbol
  when :red;   { r: 255, g: 0, b: 0 }
  when :green; { r: 0, g: 255, b: 0 }
  when :blue;  { r: 0, g: 0, b: 255 }
  else; { r: 0, g: 0, b: 0 }
  end
end

p rgb(color_symbol= :red)

Is more readable than

def rgb(color_number)
  return nil unless color_number.is_a? Fixnum
  case color_number
  when 1; { r: 255, g: 0, b: 0 }
  when 2; { r: 0, g: 255, b: 0 }
  when 3; { r: 0, g: 0, b: 255 }
  else; { r: 0, g: 0, b: 0 }
  end
end

p rgb(color_number= 2)

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.

def rgb(color_string)
  return nil unless color_string.is_a? String
  case color_string
  when "red";   { r: 255, g: 0, b: 0 }
  when "green"; { r: 0, g: 255, b: 0 }
  when "blue";  { r: 0, g: 0, b: 255 }
  else; { r: 0, g: 0, b: 0 }
  end
end

p rgb(color_string= "blue")

This is going to be less efficient than using symbols.

终弃我 2024-12-01 14:16:08

这篇文章也可能令人感兴趣 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.

满地尘埃落定 2024-12-01 14:16:08

Symbol 实例是一组有序的字符。 String 实例是字符的容器,可以包含任何字符序列的位置。

A Symbol instance is an ordered set of characters. A String instance is a container for characters, a place that can contain any sequence of characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文