Understanding the injection behavior used with lambda in Ruby

I often connect pre-configured lambdas to enumerated methods like "map", "select", etc. but the "injection" behavior seems different. for example with

mult4 = lambda {|item| item * 4 }

then

(5..10).map &mult4

gives me

[20, 24, 28, 32, 36, 40]

However, if I create a 2-parameter lambda for use with injection, for example,

multL = lambda {|product, n| product * n }

I want to say

(5..10).inject(2) &multL

since "inject" has an optional single parameter for the initial value, but that gives me ...

irb(main):027:0> (5..10).inject(2) &multL
LocalJumpError: no block given
        from (irb):27:in `inject'
        from (irb):27

However, if I write '& multL' in the second input parameter, then it works.

irb(main):028:0> (5..10).inject(2, &multL)
=> 302400

My question is: "Why does this work, and not the previous attempt?"

+5
source share
1 answer

, ,

(5..10).map &mult4

(5..10).inject(2) &multL

, ruby ​​parens ,

(5..10).map(&mult4)

,

(5..10).inject 2, &multL

parens , -.

+10

All Articles