# Rubynuby - confused by method name "inject"

**URL:** <https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183>\
**Category:** Ruby\
**Created:** [May 21, 2006, 4:42am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183 "2006-05-21T04:42:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 4:42am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/1 "2006-05-21T04:42:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 4:54am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/2 "2006-05-21T04:54:52Z")

</div>

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

> 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/](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]

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 4:58am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/3 "2006-05-21T04:58:32Z")

</div>

Hi –

On Sun, 21 May 2006, Jeff P. wrote:

> jp
> 
> P.S.  
> If anybody wants to take a stab at “collect”, that would be welcome  
> also.

I have just about the mental energy for collect right now, so I’ll  
give that a try 🙂

The basic usage (just to have an example):

You have an array: [1,2,3,4,5]  
You call: new\_array = array.collect {|element| element \* 10 }  
The result (new\_array) is: [10,20,30,40,50]

Now, in terms of the word “collect” itself:

Imagine that the elements of the first array are sitting on a bus.  
The conductor comes through the bus and _collects_ the fare from each  
element. The fare is: yourself times 10. The cumulative result is a  
new array, consisting of the old array’s elements but each multiplied  
by 10.

So collect is an iterative process, where a new value is “collected”  
from each old value, courtesy of being passed to the code block. The  
new array is thus a one-to-one mapping of the old array, with the  
values transformed. (So collect is also called “map” 🙂

David

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 5:08am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/4 "2006-05-21T05:08:00Z")

</div>

Jeff P. 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?

[“foo”, “bar”].inject(5) {|total, word| total + word.length } #=\> 11

This example injects the result of the block applied to each element of  
the collection into a single value.

I actually prefer the Haskell term, “fold” – it repeatedly folds the  
function of the value and an element of the collection back into the  
value.

> If anybody wants to take a stab at “collect”, that would be welcome  
> also.

I use “map”, which is an alias of “collect”.

ys = xs.map {|x| x \* x }

It takes a block (a mapping from X to Y) and applies it to a collection  
of Xs to map them to Ys.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 7:16am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/5 "2006-05-21T07:16:13Z")

</div>

I think the name originally came from Smalltalk.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 7:17am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/6 "2006-05-21T07:17:16Z")

</div>

BTW, I think “combine” makes the most sense to me for a synonym of  
“inject”. The block describes how to combine all of the elements into a  
single answer. vis:

total\_cost = [[pie,2.00],[coffee,1.00]].combine { |cost, [food, price]|  
cost += price }

jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 7:05am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/7 "2006-05-21T07:05:18Z")

</div>

I agree, “map” makes a lot more sense than “collect”. I’m going to just  
forget about collect and consider it “personally deprecated due to poor  
naming”.

As for Inject, nobody has come up with a wording that makes any sense to  
me yet. Does Inject have any aliases/synonyms?

thanks,  
jp

P.S.  
If I were to choose a synonym for the “inject” method, I might come up  
with “collect”. vis. “iterate over these items and collect the results  
in one answer.” It seems to me that this method does the same thing  
that the big Latin “E” symbol (Epsilon) does in Mathematics. Is there a  
name for that symbol within the math community? (I mean - besides  
Epsilon). Calculus and Physics are “twenty some odd years ago” for me.

P.P.S.  
Is it considered bad form to create your own aliases for things like  
this? For instance, if I were to create an alias for Enumerated#inject  
like “gather”, and use that all over my programs, would this be  
considered bad form, or just hunky-dory use of the language?

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 7:28am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/8 "2006-05-21T07:28:22Z")

</div>

On May 21, 2006, at 1:05 AM, Jeff P. wrote:

> If I were to choose a synonym for the “inject” method, I might come up  
> with “collect”. vis. “iterate over these items and collect the  
> results  
> in one answer.” It seems to me that this method does the same thing  
> that the big Latin “E” symbol (Epsilon) does in Mathematics. Is  
> there a  
> name for that symbol within the math community? (I mean - besides  
> Epsilon). Calculus and Physics are “twenty some odd years ago” for  
> me.

I think you mean Sigma. And I believe when you don’t refer to it as  
sigma you say “Summation”.

But I am not a mathematician, so take what I said with a grain of salt.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 8:02am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/9 "2006-05-21T08:02:22Z")

</div>

In Common Lisp there is the reduce function. I think that name  
appropriately qualifies the action involved.

(reduce (function +) (list 1 2 3 4 5 6))

(1…6).inject(0) {|c,v| c + v}

Also: yes, the “E” in math is a Sigma or summation. I wouldn’t say  
they’re quite the same since summnation always involves summing whereas  
inject/reduce are more abstract and multipurpose.

Ex:  
(reduce (function \*) (list 1 2 3 4 5 6))  
(1…6).ineject(1) {|c,v| c \* v}  
These both act more like a factorial than a sum.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 7:34am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/10 "2006-05-21T07:34:40Z")

</div>

On May 21, 2006, at 1:17 AM, Jeff P. wrote:

> jp
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

well here’s another suggestion for an alternate name:

each\_with\_state

We already have each\_with\_index. Of course this implies an alternate  
ordering of the arguments.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 8:28am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/11 "2006-05-21T08:28:12Z")

</div>

yea look into the difference between functional / imperative  
programming languages / most languages [Java, C, C++, etc] are  
imperative… inject, filter, map, et al are very common place in  
functional languages and require a different mindset than their  
imperative cousins. obviously ruby [as well as python] have  
functional elements. it’s worth taking the time to understand  
what they do / they’re very useful/powerful when dealing with  
collections…

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 9:33am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/12 "2006-05-21T09:33:07Z")

</div>

[1,2,3,4,5].inject(0){|sum,num|sum+num} =\> 15  
Which is ((((0+1)+2)+3)+4)+5

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 9:29am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/13 "2006-05-21T09:29:58Z")

</div>

Jeff P. wrote:

> As for Inject, nobody has come up with a wording that makes any sense to  
> me yet. Does Inject have any aliases/synonyms?

You can think of it as “accumulate”, since there is an “accumulator”,  
whose value is initially the argument to inject (or the first value in  
the collection), and subsequently the block value.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/14 "2006-05-21T13:17:31Z")

</div>

Hi –

On Sun, 21 May 2006, Jeff P. wrote:

> P.P.S.  
> Is it considered bad form to create your own aliases for things like  
> this? For instance, if I were to create an alias for Enumerated#inject  
> like “gather”, and use that all over my programs, would this be  
> considered bad form, or just hunky-dory use of the language?

It would be bad form, in my opinion. It’s good to discuss what you  
feel are the shortcomings of the language in a forum like this, but  
don’t turn your programs into a position statement at the cost of  
making them harder to understand.

David

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 1:30pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/15 "2006-05-21T13:30:40Z")

</div>

Jeff P. 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).

I agree!!!

I have it marked in my “Ruby notes”, as “Don’t use”.  
I do this so that when I come across it again, I won’t spend 2 hours  
trying to make sense out of it’s purpose.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 11:49am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/16 "2006-05-21T11:49:26Z")

</div>

Jeff P. wrote:

> If anybody wants to take a stab at “collect”, that would be welcome

I think #collect is fairly intuitive:

addresses = contacts.collect{|contact| contact.address}

Here we collect the addresses of the contacts.

Daniel

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/17 "2006-05-21T17:22:09Z")

</div>

On May 21, 2006, at 10:42 AM, Jeff P. wrote:

> answer this. If I understand it correctly, Matz basically “invented”  
> ruby. Matz is, as far as I know, Japanese. Was Ruby first written  
> with  
> Japanese class and method names and later translated to English?  
> Or did  
> it start out in English?
> 
> thanks,  
> jp

No, Matz has said that he wanted to create a language that could be  
used by everybody and he realized that it would have to use English.

Ruby honors its influences by reusing their names. Thus we have the  
Smalltalk’s “inject”, Lisp’s “mixin”, C’s “sprintf” and “puts”,  
Perl’s $ variables, etc.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/18 "2006-05-21T16:41:59Z")

</div>

Following David’s suggestion, I think I’ll go ahead and use it, but  
always precede it with a comment so I can remember what it does when I  
come back to it later. Something like:

# this “inject” method is “combining” the results from the block

operation into a single answer  
[1,2,3,4,5].inject(0){|sum,num|sum+num}

This topic makes me wonder, and perhaps one of the old-timers here can  
answer this. If I understand it correctly, Matz basically “invented”  
ruby. Matz is, as far as I know, Japanese. Was Ruby first written with  
Japanese class and method names and later translated to English? Or did  
it start out in English?

thanks,  
jp

ReggW wrote:

> Jeff P. 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).
> 
> I agree!!!
> 
> I have it marked in my “Ruby notes”, as “Don’t use”.  
> I do this so that when I come across it again, I won’t spend 2 hours  
> trying to make sense out of it’s purpose.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/19 "2006-05-21T18:02:04Z")

</div>

2006/5/21, ReggW [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I have it marked in my “Ruby notes”, as “Don’t use”.  
> I do this so that when I come across it again, I won’t spend 2 hours  
> trying to make sense out of it’s purpose.

Granted that it may take some time to understand it and to recognize  
its power (took me a while, too), but - once you grokked it you’ll be  
amazed how much you can do with it. A lot - if not all - methods in  
Enumerable can be elegantly implemented with inject. Try it out!  
Also, when searching the archives you’ll find quite a few postings  
that show how something can be done with inject and often it’s more  
elegant than other methods. Just compare the example that has been  
shown in this thread with the version using each:

sum = 0  
enum.each {|x| sum += x}  
sum

enum.inject(0) {|sum, x| sum + x}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [May 21, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/20 "2006-05-21T18:08:16Z")

</div>

Hi –

On Mon, 22 May 2006, Timothy H. wrote:

> > 
> 
> No, Matz has said that he wanted to create a language that could be used by  
> everybody and he realized that it would have to use English.
> 
> Ruby honors its influences by reusing their names. Thus we have the  
> Smalltalk’s “inject”, Lisp’s “mixin”, C’s “sprintf” and “puts”, Perl’s $  
> variables, etc.

A dubious honor, in the latter case; quoting the ToDo:

- discourage use of symbol variables (e.g. $/, etc.) in manual
- discourage use of Perlish features by giving warnings.

🙂

David

[Next page](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183.md?page=2)
