加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby中的符号

发布时间:2020-12-17 03:52:54 所属栏目:百科 来源:网络整理
导读:我不知道我是否理解符号是什么.我在不同的地方阅读以下内容: 符号是不可变的,这意味着内容在初始化后不会改变.这意味着对象可以有很多引用,因为内容不能更改. 将符号用于内容重要的身份和字符串. 符号对象仅实例化一次,并且只要进程运行就存在. 关于符号表
我不知道我是否理解符号是什么.我在不同的地方阅读以下内容:

>符号是不可变的,这意味着内容在初始化后不会改变.这意味着对象可以有很多引用,因为内容不能更改.
>将符号用于内容重要的身份和字符串.
>符号对象仅实例化一次,并且只要进程运行就存在.
>关于符号表

我不明白这些短语.最后,我不明白符号对象是什么.是:命名一个符号对象,或者是:namejust对其他地方的符号对象的引用?

解决方法

“ 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 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读