这个 &:last 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是以下缩写:
它是
Symbol#to_proc
方法。它曾经是 Rails ActiveSupport 的一部分,但后来被添加到 Ruby 语法中。就性能而言,我编写了一个快速基准测试脚本来了解 1.8 和 1.9 中的性能效果。
首先,在给您结果之前 - 如果您还不确定 Ruby 1.9 总体上速度有了巨大的提高,那么请做好被震撼的准备。
Ruby 1.8 结果:
Ruby 1.9 结果:
首先:哇。 Ruby 1.9 速度很快。但我们在这里得出的更相关的结论很有趣:
to_proc
显然更快。在多次运行的 1.8 中,速度有点慢。这似乎表明唯一真正的性能瓶颈是创建所有这些 Proc 对象。to_proc
方法显然比块快得多,无论执行多少次。在这种情况下,您不仅可以获得更清晰的代码,而且还可以提高性能。最后,无论您使用哪个版本,
to_proc
显然都不足以造成不值得使用的性能问题 - 事实上,它有时会加快速度!This is shorthand for:
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.
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:
Ruby 1.9 results:
First off: Wow. Ruby 1.9 is fast. But the more relevant conclusions we draw here are interesting:
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.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!