Understanding Ruby Symbols(Ruby标识符的理解)

Posted by kev Fri, 19 Aug 2005 22:11:00 GMT

Symbols in ruby are an enigma. We use them, but many don’t really understand them.

Ruby的标识符很难讲清楚。我们都用它,但很多人并不真正理解它。

So really, what is a symbol?

什么是标识符?

Simply, a symbol is something that you use to represent names and strings.

What this boils down to is a way to efficiently have descriptive names while saving the space

one would use to generate a string for each naming instance.

简单来说,标识符就是用来表示名字和字符串的。这就出现了一种有效的方法用来描述名称,为每个命名实例生成一个字符串。

The Case of Dr. Jones

Dr. Jones 的例子

Dr. Jones is a Psychologist.

He regularly uses word association tests to diagnose patients and uses ruby to keep track of everything.

 His first patient, Why, steps up to the plate:

Dr. Jones是个心理学家。他经常用word association的方法来诊断病人,并用ruby保存记录。

Dr J: Red

Why : Ruby

Dr J: Transportation

Why : Rails

Dr J: Chunky

Why : Bacon

Dr Jones creates a hash to store his data:

Dr. Jones 用一个hash来储存他的数据:

why = {"red" => "ruby", "transportation" => "rails", "chunky" => "bacon"}

Dr. Jones’s second patient, Bob, turns in his survey results:

Dr. Jones的第二个病人,Bob,套用他的公式:

bob = {"red" => "paint", "transportation" => "car", "chunky" => "fat"}

The Problem

问题

After running several hundred word association tests,

Dr. Jones begins to realize that he’s running out of memory! On a hunch, Jones runs tests in irb:

运行几百次word association测试后,Dr. Jones开始意识到内存溢出问题。马上,Jones开始在irb上运行测试:

> patient1 = { "ruby" => "red" }

> patient2 = { "ruby" => "programming" }

> patient1.each_key {|key| puts key.object_id.to_s}

211006

> patient2.each_key {|key| puts key.object_id.to_s}

203536

Well look at that, each time he creates a hash to store his information,

ruby creates a new string object in a different memory location for each key.

Fortunately, there’s an alternative.

现在来看看,每次他创建一个hash来储存他的资料,ruby创建一个新的String对象在内存的不同位置。幸运的是,有个替代的方法。

Symbols to the Rescue

标识符来拯救

Unlike strings, symbols of the same name are initialized and exist in memory only once

during a session of ruby. Symbols are most obviously useful

when you’re going to be reusing strings representing something else.

 Reproducing Dr. Jones’s tests, we are able to see this directly:

不同于字符串,在同一个ruby会话里,同一个名字的标识符只在内存里初始化一次,只存在一个副本。当你想复用字符串代表一些东西时,标识符是很有用的。重现Dr. Jones的测试,我们能够直接看到:

> patient1 = { :ruby => "red" }

> patient2 = { :ruby => "programming" }

> patient1.each_key {|key| puts key.object_id.to_s}

3918094

> patient2.each_key {|key| puts key.object_id.to_s}

3918094

Using symbols, we’ve used a single memory address to represent the word ruby in our word association tests.

Over time, this can save alot of space.

使用标识符,我们使用了一个单一的内存地址代表ruby在我们的word association测试。随着时间的推移,这可以节省大量的空间。

So I’m no shrink, when else will I want to use symbols?

Symbols are useful whenever you’re going to be reusing a word over and over to represent something else,

 whether its a key in a hash or the method you’re using in an http query.

An example from the latest and greatest web framework Ruby on Rails is its use of symbols in routes and links.

Rails defines actions within controllers to do things within the framework before rendering a web page,

so a link in Rails may look like:

字符的复用是很有好处的。不论他是hash里的一个key还是方法,你都能用在http查询里。一个来自最新版的web框架Ruby on Rails的例子,其中的标识符使用在routes和links。Rails在解释网页之前用控制器在框架里定义actions,以便一个链接能如下所示:

link_to("View Article", :controller => "articles", :action => "show", :id =>

When an application may have hundreds of links,

or at least hundreds of references to different actions and controllers,

it is significantly more efficient to use symbols than strings.

当一个程序可能有数百个链接,或至少数百个actions和控制器的关联时,用标识符比字符串将会更加有效率的。

Finally, its important to note that the usefulness of symbols is not restricted to keys in hashes.

 For example, if one was writing a http client or server they might use get and post several times

within their application, and it might be appropriate to use:

最后,更重要的是要注意到标识符的用处并不止在hash的key。例如,一个正在写的http客户端或服务器端,将要用到很多次get和post。可以这样合适的用:

do_this if query == :get

    ...

    send_message_to_server(:post,filename)

Any time a string could be used over and over, a symbol may be a good candidate for replacement.

任何时候,一个被重复利用的字符串,都可以用标识符来替代。


翻译自:

http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值