Matthew Smillie
11/15/2006 10:10:00 AM
On Nov 15, 2006, at 8:51, Gary Boone wrote:
>
> Those are several good suggestions.
>
> next sum if (i == 3)
>
> strikes me as simple and clear. Provided you know that the block
> result
> is what matters, you can stay out of trouble.
>
> Simplifying the example, the trouble with inject is that
>
> arr.inject(0) { |sum, i| sum += i }
> arr.inject(0) { |sum, i| sum + i }
>
> both produce the same result. Yuk.
>
> Maybe it should be posted somewhere as a potential 'gotcha'.
I think it could be that I'm just too familiar with the whole
accumulator thing, but I'm having trouble even imagining a case where
you'd want it any other way. Why would you want those to produce
different results, and - here I can't even guess - what would those
different results actually be?
I have a hunch that the key phrase is "Provided you know that the
block result is what matters, you can stay out of trouble." I mean,
that's sort of inherent in the construct. If you don't know that,
I'd you're going to have a lot of trouble with #inject.
m.s.