gpt4 book ai didi

ruby - Ruby 中的符号

转载 作者:数据小太阳 更新时间:2023-10-29 07:24:07 25 4
gpt4 key购买 nike

我不知道我是否理解符号是什么。我在不同的地方阅读了以下内容:

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

我不明白这些短语。最后,我不明白什么是符号对象。 :name 是符号对象,还是 :name 只是对其他地方的符号对象的引用?

最佳答案

这本书的引用 ' Eloquent Ruby '帮助我理解了符号,书中有一整章都在处理字符串和符号,这也非常好。

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 thatwe are processing. Read in those Book objects from the database and you will verylikely have your hands full of string data, things like the title of the book, the author’sname, 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 itsees it—the code needs to know which records to return, and :all is the flag that saysit should return every one. The nice thing about using something like :all for thiskind of “stands for” duty is that it also makes sense to the humans: You are a lot morelikely 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 theone 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 thewhole range of string manipulation tools at your fingertips: You might want the firstten characters of the title, or you might want to get its length or see whether it matchessome regular expression. On the other hand, if you are using some characters to standfor something in your code, you probably are not very interested in messing with theactual characters. Instead, in this second case you just need to know whether this thingis the flag that tells you to find all the records or just the first record. Mainly, whenyou want some characters to stand for something, you simply need to know if this isthe same as that, quickly and reliably.

关于ruby - Ruby 中的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com