What does the `&` mean in the following ruby syntax?

前端 未结 4 605

In the following ruby example, what does the & represent? Is it along the line of += in a loop?

payments.sum(&:price)
         


        
4条回答
  •  遥遥无期
    2020-12-04 02:59

    No, it has nothing to do with +=. The unary & operator, when used in a method call, turns the given Proc object into a block. If the operand is not a Proc (as in this case where it is a symbol), first to_proc is called on it and then the resulting Proc object is turned into a block.

提交回复
热议问题