On Jan 16, 2012, at 10:15 PM, Robert Klemme wrote:

> Please do not top post.
>=20
> 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 =
convention that the last statement in method is a return.
>=20
> ??? 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".
>=20
>> The nature of inject is to assign the last value to the memo that has =
not been used ever in your case.
>=20
> What "memo"?

Memo is the name of the first parameter that is passed to the block, or =
at least it's how it's named in doc and how i've used to call it.=20

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

The argue was about the explicit memo returns so on a given sample I =
tried to show that that sample do not necessarily need to use blocks. =
But I see no problem to explicitly return value if i was passed to that =
block. Yes, sometimes it looks ugly, sometimes, especially with tap, it =
looks pretty nice.=20

>=20
>> 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.
>>=20
>> On Jan 16, 2012, at 6:14 PM, Adam Prescott wrote:
>>=20
>>> On Jan 16, 2012 4:09 PM, "Sigurd" <cu9ypd / gmail.com> wrote:
>>>>=20
>>>> You example is not accurate though:
>>>>=20
>>>> 5 +  [1, 2, 3, 4].reduce(&:+)
>=20
> 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?
>=20

Technically it's good. My argument was not that the code is not =
functional or doing something in a wrong way or that syntax is =
unacceptable. The point i have is that Adams code is not the best =
example illustrating that the explicit inject block returns is the sign =
of incorrect inject use or let's call it "code smell". I'm not against =
that particular piece of code, it's just someone is wrong in internet =
again :)

> 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], []]
>=20
> Both have their use and it depends on the use case which you pick.
>=20

I don't know can this be called different behavior. Inject use the first =
value of collection if it's not set explicitly so it's like particular =
case of inject, but i'm ok with the two different cases as well.

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