Today's the day

向软件大牛炫耀我会焊单片机,向硬件大牛炫耀我会写 Rails,向软硬件大牛炫耀我生物,向软硬件生物大牛炫耀我会折腾期货 -_-bbb

Ruby中 respond_to? 和 send 的用法
Ruby中的数组过滤

Ruby中的字符串与符号

galeki posted @ 2007年7月16日 00:00 in RoR with tags string symbol 符号 字符串 ruby , 7093 阅读

初学Ruby的时候,往往对字符串(String类)与符号(Symbol)这两种变量类型分不清楚,其实,我们大可不必纠缠这两种变量类型的内部实现,只需记住它们的特点和区别就可以了:

1. 字符串和符号,都是Ruby中表示文本的方式

不管字符串变量也好,符号变量也好,都是用来表示文本的,它们之间,也可以互相转换(通过to_sym和to_s):

  1. >>  "book".to_sym
  2. => :book
  3. >>  :track.to_s
  4. => "track"

2. 相同的符号是一个对象,相同的字符并不一定是一个对象

这就是符号和字符串最大的差别,符号是不可变的,不能对一个符号进行修改。所以说,两个看起来相同的符号一定是一个对象,但是说两个看起来一样的字符串是一个对象,就不对了。

这里,我们用 equal? 方法检验这一点,当两个对象相同时,equal? 返回true,反之是false:

  1. >>  :book.equal?(:book)
  2. => true
  3. >>  "book".equal?("book")
  4. => false

3. 处理符号相比字符串,占用更少的资源

这也就是为什么有些时候我们要使用符号的原因,由于字符串变量必须具有各种修改其内容的功能,所以字符串的维护和处理的开销就很大,但是有些时候,我们并不需要修改和处理创建的文本信息,这个时候用符号再好不过了。

比较典型的用法,就是在Rails中,用符号来指代散列中的参数名称:

  1. <% for article in @articles -%>
  2.     <%= link_to article.title,
  3.                    :controller => "article",
  4.                    :action      => "show",
  5.                    :id             => article.id   %>
  6. <% end -%>

反之,如果我们把 :controller 写成"controller",把 :action 写成"action",吧 :id 写成"id",每一个循环都要创建3个新的字符对象,但是我们又不需要对这些字符串进行修改操作,白白添加了很多处理器开销,而且写成符号的形式更加简练,可以更容易分别出参数和值,何乐而不为呢。

meidir 说:
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

meidir 说:
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

meidir 说:
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

meidir 说:
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

meidir 说:
2022年9月06日 00:08

Merely a smiling visitant here to share the love (:, btw outstanding style. Bounce house cost

meidir 说:
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

meidir 说:
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?

meidir 说:
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

meidir 说:
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

meidir 说:
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

meidir 说:
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

meidir 说:
2022年9月07日 19:09

I just love to read new topics from you blog. is god real


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter