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.
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, how inject is
working to binary operation + and how it passes the value of enum and
where the operation is taking happening to produce final operation `10`
?
···
--
Posted via http://www.ruby-forum.com/\.