On May 20, 2006, at 10:42 PM, Jeff Pritchard wrote:

> Can anybody explain to me how the Enumberable#inject method is
> "injecting" something into something?  I find it very difficult  
> remember
> method names when I don't "get" them.  So far in Ruby, "inject" takes
> the cake for least understandable method name (with my own particular
> convoluted gray matter).
>
> Can somebody give some examples of its use and state in words how  
> it is
> "injecting" something into something?
>
> thanks,
> jp
>
> P.S.
> If anybody wants to take a stab at "collect", that would be welcome
> also.
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

Would you prefer foldl? :-p

Well one possible explanation is that it's injecting the contents of  
the array and the result of the last call to the block back into the  
block, sort of an auto-transfusion.

The canonical inject example is the sum of the elements of an array:
irb(main):001:0> [1, 7, 2].inject(0) { |sum_so_far, current_item|  
sum_so_far + current_item }
=> 10

As far as collect goes, you're collecting the results of the block  
applied to each element in the enumerable. (I personally prefer map  
to collect as far as terminology goes).

irb(main):002:0> [1, 7, 2].collect { |item| item * 2 }
=> [2, 14, 4]