# 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:** 2

<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:45pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/21 "2006-05-21T18:45:09Z")

</div>

Ruby’s inject() lets you do amazing things very expressively. I first  
got into inject() when realized how easy it makes getting a class by its  
fully qualified name:

klass = fully\_qualified\_name.split(’::’).inject(Module) { |\_module,  
\_symbol|  
\_module.const\_get(\_symbol)  
}

instance = klass.new

In this example name “inject” totally corresponds to what it is doing  
here. To me at least.

Sincerely,  
Gennady.

---

<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:55pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/22 "2006-05-21T18:55:17Z")

</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?

How I remember: Inject takes a binary operation (e.g. +) and injects it  
between each element of a list.

[1,2,3].inject { |a,b| a+b } =\> 1+2+3

– Jim W.

---

<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 22, 2006, 11:28am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/23 "2006-05-22T11:28:05Z")

</div>

On 5/21/06, Jim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?
> 
> How I remember: Inject takes a binary operation (e.g. +) and injects it  
> between each element of a list.
> 
> [1,2,3].inject { |a,b| a+b } =\> 1+2+3

Awesome! That really helps me!

---

<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 22, 2006, 12:04pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/24 "2006-05-22T12:04:06Z")

</div>

Leslie V. wrote:

> On 5/21/06, Jim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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?
> > 
> > How I remember: Inject takes a binary operation (e.g. +) and injects it  
> > between each element of a list.
> > 
> > [1,2,3].inject { |a,b| a+b } =\> 1+2+3
> 
> Awesome! That really helps me!

Seems like “sum” would have been a better name.

Also

[1,2,3].each {|a| b += a}

is easier to read …for me.

---

<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:54pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/25 "2006-05-21T23:54:05Z")

</div>

Jim W. wrote:

> How I remember: Inject takes a binary operation (e.g. +) and injects it  
> between each element of a list.
> 
> [1,2,3].inject { |a,b| a+b } =\> 1+2+3
> 
> – Jim W.

I think about this in a slightly different way, which helps me  
rememeber. Something like, “#inject itorates over thie items in order  
but also _injects_ the result from the last block call into the current  
one.”

(1…5).inject { |injected\_value, this\_item| injected\_value + this\_item  
} =\> 15

---

<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 22, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/26 "2006-05-22T12:17:02Z")

</div>

2006/5/22, Regg Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Seems like “sum” would have been a better name.

Definitively not because you can do far more than calculating sums with  
inject.

> Also
> 
> [1,2,3].each {|a| b += a}
> 
> is easier to read …for me.

Yes, but it also needs more LOC.

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 22, 2006, 12:16pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/27 "2006-05-22T12:16:54Z")

</div>

On 5/22/06, Regg Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Awesome! That really helps me!
> 
> Seems like “sum” would have been a better name.

Or an alias inject\_operator perhaps…  
This thread has discussed “sum” already and it’s no good because you  
can use any operator. I also like “accumulate” as mentioned, but  
inject\_operator is sooo clear to me.

---

<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 22, 2006, 1:36pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/28 "2006-05-22T13:36:09Z")

</div>

On 5/22/06, Leslie V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > [1,2,3].inject { |a,b| a+b } =\> 1+2+3
> > > 
> > > Awesome! That really helps me!
> > 
> > Seems like “sum” would have been a better name.
> 
> Or an alias inject\_operator perhaps…  
> This thread has discussed “sum” already and it’s no good because you  
> can use any operator. I also like “accumulate” as mentioned, but  
> inject\_operator is sooo clear to me.

…though the other uses of inject defy “inject\_operator”…

data = [[1, 7, 3], [1, 2], [1, 5, 4, 1], [1, 8]]

longest\_sample = nil  
longest\_length = data.inject(0) do |accum, sample|  
if accum \> sample.length  
accum  
else  
longest\_sample = sample  
sample.length  
end  
end

puts “Longest sample: #{longest\_sample.inspect}, length  
#{longest\_length}”

…then again, using inject to find longest seems a bit silly…

longest\_sample = data[0]  
data.each do |sample|  
longest\_sample = sample if sample.length \> longest\_sample.length  
end  
puts “Longest sample: #{longest\_sample.inspect}, length  
#{longest\_length}”

---

<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 22, 2006, 1:42pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/29 "2006-05-22T13:42:35Z")

</div>

> …then again, using inject to find longest seems a bit silly…
> 
> longest\_sample = data[0]  
> data.each do |sample|  
> longest\_sample = sample if sample.length \> longest\_sample.length  
> end  
> puts “Longest sample: #{longest\_sample.inspect}, length #{longest\_length}”

longest\_sample = data.max{|a,b| a.length \<=\> b.length}

---

<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 22, 2006, 1:42pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/30 "2006-05-22T13:42:21Z")

</div>

Hi –

On Mon, 22 May 2006, Regg Mr wrote:

> > Awesome! That really helps me!
> 
> Seems like “sum” would have been a better name.

Only in the case where you’re calculating a sum 🙂 Inject does the  
sum-like thing but in a generalized way.

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 22, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/31 "2006-05-22T13:58:00Z")

</div>

This is a question that I have often wondered about and I think this  
thread  
has gone a fair way to explaining it.

Just to make sure, I want to step through a call to inject (The sum  
example)  
to see that I have it. Any comments would be great.

[1,2,3].inject(0) { |cur\_sum, elem| cur\_sum + elem }

Iteration 1  
cur\_sum = 0, elem = 1  
cur\_sum = cur\_sum + elem #=\> cur\_sum = 0 + 1 (Replace cur\_sum with the  
results of the block =\> 1 )

Iteration 2  
cur\_sum = 1, elem = 2  
cur\_sum = cur\_sum + elem #=\> cur\_sum = 1 + 2 (Replace cur\_sum with the  
results of the block =\> 3 )

Iteration 3  
cur\_sum = 3, elem = 3  
cur\_sum = cur\_sum + elem #=\> cur\_sum = 3 + 3 (Replace cur\_sum with the  
results of the block =\> 6 )

Return cur\_sum #=\> 6

I think that part so far is ok. I get this part. but how would I step  
through this?

klass = fully\_qualified\_name.split(’::’).inject(Module) { |\_module,  
\_symbol|  
\_module.const\_get(\_symbol)  
}

instance = klass.new

---

<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 22, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/32 "2006-05-22T15:48:40Z")

</div>

2006/5/22, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> cur\_sum = cur\_sum + elem #=\> cur\_sum = 0 + 1 (Replace cur\_sum with the  
> results of the block =\> 6 )
> 
> Return cur\_sum #=\> 6
> 
> I think that part so far is ok. I get this part. but how would I step  
> through this?

Exactly the same - only values are different.

> klass = fully\_qualified\_name.split(‘::’).inject(Module) { |\_module,  
> \_symbol|  
> \_module.const\_get(\_symbol)  
> }
> 
> instance = klass.new

> > class Foo;class Bar;class Baz; end end end  
> > =\> nil  
> > “Foo::Bar::Baz”.split(/::/).inject(Object) {|cl,n| print "-\> ", cl,  
> > ", ", n, “\n”; cl.const\_get(n)}.new  
> > → Object, Foo  
> > → Foo, Bar  
> > → Foo::Bar, Baz  
> > =\> #Foo::Bar::Baz:0x3a3178

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 22, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/33 "2006-05-22T16:01:53Z")

</div>

Regg Mr wrote:

Two points:

> Seems like “sum” would have been a better name.

Unless the operator isn’t + …

```
[1,2,3].inject { |left, right| left * right }
  # => 1*2*3

"A::B".split("::").inject(Object) { |left, right| 

```

left.const\_get(right) }  
# =\> Object.const\_get(“A”).const\_get(“B”)

```
(2..n).inject(1) { |left, right| left * right }
  # => n! => 2*3* ... * n

```

> Also
> 
> [1,2,3].each {|a| b += a}
> 
> is easier to read …for me.

Perhaps, but there is a fundamental difference between the inject and  
each versions. The “each” verison is procedural. It defines state (the  
value of b) and how that state changes in each iteration (b += a).

The “inject” version is functional. There are no extra state variables  
in our code that need initialization. And the expression itself is the  
value, rather than having a side effect on a local variable.

Not that one way is better than the other, just noting differences.

– Jim W.

---

<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 22, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/34 "2006-05-22T16:59:40Z")

</div>

> ```
> sample.length
> 
> ```
> 
> end  
> end
> 
> puts “Longest sample: #{longest\_sample.inspect}, length  
> #{longest\_length}”
> 
> …then again, using inject to find longest seems a bit silly…

Well, Enumerable#max is the way to go here, but inject isn’t  
that clumsy too:

longest = data.inject{|max, this| this.length \> max.length ? this : max}  
puts “Longest sample: #{longest.inspect}, length #{longest.length}”

I love it, it’s the swiss army knife of Enumerable.

cheers

Simon

---

<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 24, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/35 "2006-05-24T18:24:37Z")

</div>

On 5/21/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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.

The big Sigma is often called the n-ary sum operator, and there’s a  
corresponding big Pi called the n-ary product (it can be implemented  
as inject where the operation in the block is \*).

So, in math-speak, I think one could say:

inject applies a block pairwise as an n-ary operation over an  
Enumerable, with an optional initial value.

This may or may not help anyone understand it. 🙂

-A

---

<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 24, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/36 "2006-05-24T21:48:44Z")

</div>

“Kroeger, Simon (ext)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Well, Enumerable#max is the way to go here, but inject isn’t  
> that clumsy too:  
> longest = data.inject{|max, this| this.length \> max.length ? this : max}

longest = data.inject{|max, this| [this.length, max.length].max }

😉 (That’s how they do it in APL too, btw.)

---

<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 24, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/37 "2006-05-24T22:08:17Z")

</div>

Christian N. schrieb:

> longest = data.inject{|max, this| [this.length, max.length].max }
> 
> 😉 (That’s how they do it in APL too, btw.)

I know why I prefer Ruby 😉

data = %w"hello christian and APL"  
p data.inject{|max, this| [this.length, max.length].max }

# =\> undefined method `length’ for 9:Fixnum (NoMethodError)

Regards,  
Pit

---

<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 22, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/38 "2006-05-22T13:54:55Z")

</div>

On 5/22/06, Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > …then again, using inject to find longest seems a bit silly…
> > 
> > longest\_sample = data[0]  
> > data.each do |sample|  
> > longest\_sample = sample if sample.length \> longest\_sample.length  
> > end  
> > puts “Longest sample: #{longest\_sample.inspect}, length #{longest\_length}”
> 
> longest\_sample = data.max{|a,b| a.length \<=\> b.length}

Yes precisely! That’s what I like about Ruby, everyone’s a smartypants!  
(one day I will be too!)

---

<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 24, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/39 "2006-05-24T23:44:26Z")

</div>

Hi,

On 5/24/06, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # =\> undefined method `length’ for 9:Fixnum (NoMethodError)

This works:  
p data.inject(0){|max, this| [this.length, max].max }

And in APL it’s 4 Unicode characters plus the symbol:  
(max)(reduce)(shape)(each)data,  
where (reduce) is an operator on (max) producing the function which  
is equivalent to Ruby’s inject method.

So there … 🙂

Stuart

---

<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 25, 2006, 4:05am UTC](https://www.ruby-forum.com/t/rubynuby-confused-by-method-name-inject/61183/40 "2006-05-25T04:05:41Z")

</div>

I must say I’m surprised that my tired old thread is still kicking  
around.

If I may summarize, the reason I was confused by Inject is that it is  
useful but poorly named.

many thanks for all the replies,  
jp

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

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