Ruby中的字符串与符号
初学Ruby的时候,往往对字符串(String类)与符号(Symbol)这两种变量类型分不清楚,其实,我们大可不必纠缠这两种变量类型的内部实现,只需记住它们的特点和区别就可以了:
1. 字符串和符号,都是Ruby中表示文本的方式
不管字符串变量也好,符号变量也好,都是用来表示文本的,它们之间,也可以互相转换(通过to_sym和to_s):
-
>> "book".to_sym
-
=> :book
-
>> :track.to_s
-
=> "track"
2. 相同的符号是一个对象,相同的字符并不一定是一个对象
这就是符号和字符串最大的差别,符号是不可变的,不能对一个符号进行修改。所以说,两个看起来相同的符号一定是一个对象,但是说两个看起来一样的字符串是一个对象,就不对了。
这里,我们用 equal? 方法检验这一点,当两个对象相同时,equal? 返回true,反之是false:
-
>> :book.equal?(:book)
-
=> true
-
>> "book".equal?("book")
-
=> false
3. 处理符号相比字符串,占用更少的资源
这也就是为什么有些时候我们要使用符号的原因,由于字符串变量必须具有各种修改其内容的功能,所以字符串的维护和处理的开销就很大,但是有些时候,我们并不需要修改和处理创建的文本信息,这个时候用符号再好不过了。
比较典型的用法,就是在Rails中,用符号来指代散列中的参数名称:
-
<% for article in @articles -%>
-
<%= link_to article.title,
-
:controller => "article",
-
:action => "show",
-
:id => article.id %>
-
<% end -%>
反之,如果我们把 :controller 写成"controller",把 :action 写成"action",吧 :id 写成"id",每一个循环都要创建3个新的字符对象,但是我们又不需要对这些字符串进行修改操作,白白添加了很多处理器开销,而且写成符号的形式更加简练,可以更容易分别出参数和值,何乐而不为呢。
2022年8月27日 22:00
It is trendy write-up in addition to i love to to learn to read that write-up. your site is usually excellent therefore you include beneficial workers with your blog site. pleasant giving thanks. bolsas ecologicas de tela
2022年8月31日 20:06
This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. Thank you flower delivery Singapore
2022年9月06日 00:06
Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info. Water damage Spokane wa
2022年9月06日 00:07
Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work. Chair and table rentals sacramento
2022年9月06日 00:08
Merely a smiling visitant here to share the love (:, btw outstanding style. Bounce house cost
2022年9月06日 00:09
You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. Bounce house sacramento
2022年9月06日 00:09
Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging.. How old is god?
2022年9月06日 00:10
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future... What is the five fold ministry
2022年9月06日 00:11
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. flower delivery seattle, WA
2022年9月06日 00:12
I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. commercial window tinting sacramento
2022年9月06日 00:12
Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. sell my house fast seattle washington
2022年9月07日 19:09
I just love to read new topics from you blog. is god real