Confusion with Ruby's enum#inject

From the doc about the syntax : inject {| memo, obj | block } -> obj

:: If you specify a block, then for each element in enum the block is
passed an accumulator value (memo) and the element

[1, 2, 3, 4].inject() { |result, element| p “executing” ; p “#{result} =>
#{element}” ; result + element }
“executing”
“1 => 2”
“executing”
“3 => 3”
“executing”
“6 => 4”
=> 10

Now while the above is understood very well. below is a bit confusing
me.

Again from the Doc

inject(sym) -> obj

:: If you specify a symbol instead, then each element in the collection
will be passed to the named method of memo.

[1,2,4,3].inject(:+)
=> 10

Couldn’t understand the line within **. With symbol only -

(a)How inject is performing the binary operation + ?

(b) How does inject pass the value of enum and

© Where the operation is being performed to the produce final result
as
10 ?

Thnanks

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

works the “same” as
[1, 2, 3, 4].inject() { |result, element| result.send(:+,element) }

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs