How does this ruby injection magic work?

前端 未结 3 1527
余生分开走
余生分开走 2020-12-11 06:49

I saw a ruby code snippet today.

[1,2,3,4,5,6,7].inject(:+)  

=> 28

[1,2,3,4,5,6,7].inject(:*)  

3条回答
  •  情歌与酒
    2020-12-11 07:23

    As you can see in the docs, inject can take a block (like you're familiar with) or a symbol that represents the name of a binary operator. It's a useful shorthand for already-defined ops.

提交回复
热议问题