(&:id)在Product.all.map(&:id)中到底做了什么 [英] What exactly does (&:id) do in Product.all.map(&:id)
本文介绍了(&:id)在Product.all.map(&:id)中到底做了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我想要绕过的代码行:
Here's the line of code I'm trying to wrap my head around:
Category.all.map(&:id).each { |id| Category.reset_counters(id, :products) }
希望某人可以帮助我了解(&:id)
的功能以及它如何影响其余部分?我相信它会将符号:id
转换为会响应id
的 proc ?!?但这会令人困惑...
Hoping someone can help me understand what (&:id)
is doing and how it impacts the rest of the line? I believe it turns the symbol :id
into a proc that'll respond to id
?!? But then it gets confusing...
提前谢谢!
推荐答案
Category.all.map(&:id)
是
Category.all.map { |a| a.id }
关于它如何影响其余的行,以上部分将所有id
值作为单个Array
返回.然后,将这个Array
id传递给另一个对each
的调用,该调用将每个id迭代地传递给reset_counters
.
as for how it affects the rest of the line, the above section returns all id
values as a single Array
. This Array
of ids is then passed into another call to each
, which iteratively passes each id into reset_counters
.
这篇关于(&:id)在Product.all.map(&:id)中到底做了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文