Hacker News new | ask | show | jobs
by kommissar 6683 days ago
What does the &: do in Ruby?
2 comments

Tokens beginning with a colon are literal Ruby symbols.

Prepending an ampersand before the last argument of a method call means that argument should be converted into a Proc and sent as a callback block to that method.

Actually, it's &(:+) not &:(+). The & takes its argument and converts from a Proc to a block. :+ is a symbol that implements the method #to_proc to give you a block that sums its receiver and its argument.
Oh, cool. I knew the colon meant a Ruby symbol, but I did not know that you could use & to convert a Proc to block.