この二つは同じ
[1,2,3].inject(5){|init, v| init + v}
[1,2,3].inject(5, &:+)
追記;これも同じ
[1,2,3].inject(5, :+)
block、proc、&の説明
& operator to convert a symbol to a Proc and then to a block:
names = ['bob', 'bill', 'heather']
//これは全て同じ結果
names.map {|name| name.capitalize }
names.map(&:capitalize.to_proc)
names.map(&:capitalize)
to_procは何をやっているのか?
class Symbol
def to_proc
Proc.new {|x| x.send(self) }
end
end