这个 &:last Ruby 结构叫什么?

发布于 2024-08-20 15:07:59 字数 337 浏览 1 评论 0原文

可能的重复:
map(&:name) 在 Ruby 中意味着什么?

什么是事物就像调用 survey.map(&:questions).flatten.compact 一样,这样我就可以找到有关它们的更多信息:)。 &: 解决了什么问题,或者它到底在做什么?它被用在其他语言中吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相对绾红妆 2024-08-27 15:07:59

这是以下缩写:

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 结果:

                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 结果:

                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)

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

  • 在这两种情况下,仅运行一次,to_proc 显然更快。在多次运行的 1.8 中,速度有点慢。这似乎表明唯一真正的性能瓶颈是创建所有这些 Proc 对象。
  • 然而,在 Ruby 1.9 中,to_proc 方法显然比块快得多,无论执行多少次。在这种情况下,您不仅可以获得更清晰的代码,而且还可以提高性能。

最后,无论您使用哪个版本,to_proc 显然都不足以造成不值得使用的性能问题 - 事实上,它有时会加快速度!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文