Please do not top post.

On Mon, Jan 16, 2012 at 7:08 PM, Sigurd <cu9ypd / gmail.com> wrote:
> it seems not quite accurate to me because of block. inject uses conventio=
n that the last statement in method is a return.

??? Inject uses the convention that the block passes the "aggregator"
on which it has received as first argument.  This does not have
anything to do with "return".

> The nature of inject is to assign the last value to the memo that has not=
 been used ever in your case.

What "memo"?

> =A0Therefore it's more natural to use short inject method definitions: ei=
ther a.inject(5, :+) either 5 + a.inject(:+). =A0If the memo return in proc=
 would be unnatural the inject won't pass it to the proc explicitly.

???

> On the other side I'm not a proponent of the crazy injects that could be =
barely understood. I think in this case inject could be used easily as well=
 as the other solutions provided.
>
> On Jan 16, 2012, at 6:14 PM, Adam Prescott wrote:
>
>> On Jan 16, 2012 4:09 PM, "Sigurd" <cu9ypd / gmail.com> wrote:
>>>
>>> You example is not accurate though:
>>>
>>> 5 + =A0[1, 2, 3, 4].reduce(&:+)

Can you please again explain what is not accurate about Adam's piece
of code?  Are you aware of the two different behaviors of #inject?

irb(main):001:0> [[1,2,3],[1],[]].each do |a|
irb(main):002:1* p a, a.inject(&:+), a.inject(0, &:+)
irb(main):003:1> end
[1, 2, 3]
6
6
[1]
1
1
[]
nil
0
=3D> [[1, 2, 3], [1], []]

Both have their use and it depends on the use case which you pick.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/