这个&:最后一个Ruby结构是什么? [英] What is this &:last Ruby construct called?

查看:86
本文介绍了这个&:最后一个Ruby结构是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

map(&:name)是什么意思?


调用了 survey.map(&:questions).flatten.compact 这样的东西,所以我可以找到更多关于它们的信息:)。 &:解决什么问题,或者它正在做什么?


b b b b b b b b pre> survey.map {| s | s.questions} .flatten.compact

这是 Symbol#to_proc 方法。它以前是Rails的ActiveSupport的一部分,但已被添加到Ruby语法。



至于性能,我写了一个快速基准脚本,在1.8和1.9中的性能效果的想法。

  require'benchmark'

many = 500
a =(1..10000).to_a

Benchmark.bm do | x |
x.report('block once'){a.map {| n | n.to_s}}
x.report('to_proc once'){a.map(&:to_s)}
x.report('block many'){many.times {a.map {| n | n.to_s}}}
x.report('to_proc many'){many.times {a.map(&:to_s)}}
end

首先,在给你的结果之前 - 如果你还不能确定Ruby 1.9是一个巨大的速度改进,准备吹



Ruby 1.8结果:



  b $ b块一次0.020000 0.000000 0.020000(0.016781)
to_proc一次0.010000 0.000000 0.010000(0.013881)
块许多6.680000 1.100000 7.780000(7.780532)
to_proc许多7.370000 0.540000 7.910000(7.902935)



Ruby 1.9结果:



 用户系统总实际
块一次0.010000 0.000000 0.010000(0.011433)
to_proc一次0.000000 0.000000 0.000000(0.004929)
块许多4.060000 0.000000 4.060000(4.057013)
to_proc many 2.810000 0.000000 2.810000(2.810312)

首先:Wow。 Ruby 1.9速度快。但是我们在这里得出的更相关的结论很有意思:




  • 在这两种情况下,只有一个运行, to_proc 显然更快。在1.8次的多次运行中,它的速度慢了。这似乎表明,唯一真正的性能瓶颈是创建所有这些Proc对象。

  • 然而,在Ruby 1.9中, to_proc 显然比块块快,不管你做了多少次。在这种情况下,您不仅可以获得更简洁的代码,还可以提高性能。



're use, to_proc 显然不够的性能问题是值得不使用 - 事实上,它有时加速了事情!


Possible Duplicate:
What does map(&:name) mean in Ruby?

What are things like survey.map(&:questions).flatten.compact called, so I can find more information about them :). What problems does that &: solve, or what is it doing exactly? Is it used in other languages?

解决方案

This is shorthand for:

survey.map { |s| s.questions }.flatten.compact

It's the Symbol#to_proc method. It used to be a part of Rails' ActiveSupport, but has since been added to Ruby syntax.

As far as performance goes, I wrote a quick benchmark script to get an idea of performance effect in both 1.8 and 1.9.

require 'benchmark'

many = 500
a = (1..10000).to_a

Benchmark.bm do |x|
  x.report('block once') { a.map { |n| n.to_s } }
  x.report('to_proc once') { a.map(&:to_s) }
  x.report('block many') { many.times { a.map { |n| n.to_s } } }
  x.report('to_proc many') { many.times { a.map(&:to_s) } }
end

First off, before giving you the results - if you weren't already sure that Ruby 1.9 was a huge speed improvement in general, prepare to be blown away.

Ruby 1.8 results:

                user        system      total       real
block once      0.020000    0.000000    0.020000    (  0.016781)
to_proc once    0.010000    0.000000    0.010000    (  0.013881)
block many      6.680000    1.100000    7.780000    (  7.780532)
to_proc many    7.370000    0.540000    7.910000    (  7.902935)

Ruby 1.9 results:

                user        system      total       real
block once      0.010000    0.000000    0.010000    (  0.011433)
to_proc once    0.000000    0.000000    0.000000    (  0.004929)
block many      4.060000    0.000000    4.060000    (  4.057013)
to_proc many    2.810000    0.000000    2.810000    (  2.810312)

First off: Wow. Ruby 1.9 is fast. But the more relevant conclusions we draw here are interesting:

  • In both cases, for only one run, to_proc is clearly faster. In 1.8 on the many-times run, it's tad slower. This seems to indicate that the only real performance bottleneck is creating all those Proc objects.
  • In Ruby 1.9, however, the to_proc method is clearly much faster than blocks, no matter how many times you do it. In this case, you not only get cleaner code, but improved performance, as well.

In the end, no matter which version you're using, to_proc is clearly not enough of a performance issue to be worth not using - in fact, it sometimes speeds things up!

这篇关于这个&:最后一个Ruby结构是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆