News for hackers. Stuff that matters.
Re:(1..100).inject(&:+) by kommissar on Monday February 25, 2008 @04:17PM (#123350)
What does the &: do in Ruby?
Re:(1..100).inject(&:+) by Darmani on Monday February 25, 2008 @04:27PM (#123362)
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.

Re:(1..100).inject(&:+) by raganwald on Monday February 25, 2008 @04:26PM (#123358)
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.
Re:(1..100).inject(&:+) by kommissar on Monday February 25, 2008 @05:53PM (#123426)
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.