# Choice of the word "inject" vs what the method actually does

**URL:** <https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535>\
**Category:** Ruby\
**Created:** [July 4, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535 "2007-07-04T14:37:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 4, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/1 "2007-07-04T14:37:02Z")

</div>

The word Inject essentially means to introduce something into a  
system and yet the inject method iterates over a series elements  
carrying forward some sort of result at each next step.

Hence would it not make more sense to name it " accumulate" which  
seems to me more explicit and intuitive?

Yes I realize that an alias may be used for this, however, I am  
looking for insight as to the reason “inject” was chosen over  
something better suited like accumulate.

(Eg. I am not looking for “just use alias” or “it’s tradition” or  
“that’s just how it is” as answers.)

Thank you very much for any insight and/or good discussion on this.

~Wayne

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 4, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/2 "2007-07-04T15:28:08Z")

</div>

Hi,

In message “Re: Choice of the word “inject” vs what the method actually  
does (method naming)”  
on Wed, 4 Jul 2007 21:32:06 +0900, “Wayne E. Seguin”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Yes I realize that an alias may be used for this, however, I am  
|looking for insight as to the reason “inject” was chosen over  
|something better suited like accumulate.

It a method name used in Smalltalk. Probably the primary intention  
was to pun with other -ect methods such as select, correct, or detect.  
In short, it’s tradition.

|(Eg. I am not looking for “just use alias” or “it’s tradition” or  
|“that’s just how it is” as answers.)

Oops, it might be the answer you were looking for. 😉

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 4, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/3 "2007-07-04T15:58:13Z")

</div>

On 7/4/07, Wayne E. Seguin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hence would it not make more sense to name it " accumulate" which  
> seems to me more explicit and intuitive?

You’ve already been given an answer as to why it’s named as it is, but  
if hypotethically it should be renamed, wouldn’t “foldl” be more  
suitable? As far as I can gather, inject is what functional  
programming languages call a left fold, equivalent to the Haskell  
function

foldl :: (a → b → a) → a → [b] → a

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 6, 2007, 12:02am UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/4 "2007-07-06T00:02:35Z")

</div>

On Jul 4, 9:26 am, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> was to pun with other -ect methods such as select, correct, or detect.  
> In short, it’s tradition.
> 
> |(Eg. I am not looking for “just use alias” or “it’s tradition” or  
> |“that’s just how it is” as answers.)
> 
> Oops, it might be the answer you were looking for. 😉
> 
> ```
> matz.
> 
> ```

Thank you very much! You did answer my question of where it comes  
from, the reasons.

I was not looking for “that’s just the way it’s done” which is a bit  
different than what I said 😉

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 4, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/5 "2007-07-04T16:27:18Z")

</div>

Wayne E. Seguin \<wayneeseguin [gmail.com](http://gmail.com)\> writes:

> The word Inject essentially means to introduce something into a  
> system and yet the inject method iterates over a series elements  
> carrying forward some sort of result at each next step.
> 
> Hence would it not make more sense to name it " accumulate" which  
> seems to me more explicit and intuitive?

The best I could ever come up with is that the function is `injected`  
between  
the elements of the object

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 9, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/6 "2007-07-09T21:03:39Z")

</div>

On Jul 09, 2007, at 14:53 , Gary W. wrote:

> Gary W.

Now that makes perfect sense. Thank you Gary.

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 9, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/7 "2007-07-09T20:55:38Z")

</div>

On Jul 4, 2007, at 10:26 AM, Gareth A. wrote:

> `injected` between  
> the elements of the object

Yes. For me, this rationale becomes clear when you see that

```
[1,2,3,4].inject { |a,i| a + i }

```

is the same as

```
(1 + 2 + 3 + 4)

```

The binary operator ‘plus’ has been injected between all the elements  
of the collection.

Gary W.

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 12, 2007, 2:47am UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/8 "2007-07-12T02:47:05Z")

</div>

On Jul 11, 2007, at 04:33 , C Erler wrote:

> The general idea is that you have an unfinished result. You inject  
> each item one at a time into the result.

Thank you. This also makes a lot of sense.

---

<div class="post-metadata">

**Author:** ![Wayne\_ESSeguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@Wayne\_ESSeguin](https://www.ruby-forum.com/u/Wayne_ESSeguin)\
**Post date:** [July 11, 2007, 10:34am UTC](https://www.ruby-forum.com/t/choice-of-the-word-inject-vs-what-the-method-actually-does/108535/9 "2007-07-11T10:34:09Z")

</div>

On Jul 4, 7:32 am, “Wayne E. Seguin” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes I realize that an alias may be used for this, however, I am  
> looking for insight as to the reason “inject” was chosen over  
> something better suited like accumulate.

The general idea is that you have an unfinished result. You inject  
each item one at a time into the result.

[1, 2, 3].inject(0) { |sum, item| sum + item } injects each item into  
the sum one at a time.  
[String, Array, Hash].inject({}) { |class\_lookup, klass|  
class\_lookup[klass.name] = klass } injects an entry into the lookup  
table one at a time.  
[egg, sugar, salt].inject { |mixture, ingredient| mixture.mix\_in  
ingredient } injects an ingredient into the mixture one at a time.
