8 ms·
What does the &: do in Ruby?
by kommissar 19y ago
What does the &: do in Ruby?
- raganwald 19y agoActually, 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.
- kommissar 19y agoOh, cool. I knew the colon meant a Ruby symbol, but I did not know that you could use & to convert a Proc to block.
- Darmani 19y agoTokens 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.