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
(c) Where the operation is being performed to the produce final result
as
`10` ?
Thnanks
on 2013-03-16 09:37
on 2013-03-16 09:56
[1,2,4,3].inject(:+)
works the "same" as
[1, 2, 3, 4].inject() { |result, element| result.send(:+,element) }
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.