# Inject's pathological case

**URL:** <https://rubytalk.org/t/injects-pathological-case/45508>\
**Category:** ruby-talk\
**Created:** [30 March 2008 05:15 UTC](https://rubytalk.org/t/injects-pathological-case/45508 "2008-03-30T05:15:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Just\_Another\_Victim1](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Just\_Another\_Victim1](https://rubytalk.org/u/Just_Another_Victim1)\
**Post date:** [30 March 2008 05:15 UTC](https://rubytalk.org/t/injects-pathological-case/45508/1 "2008-03-30T05:15:07Z")

</div>

I'm not sure how I feel about inject's pathological case. What do you  
all think should happen in the following code?

[2].inject {|a, i| puts i}

&nbsp;&nbsp;&nbsp;&nbsp;I might have thought that the block would never be called and the method  
would return 2.  
&nbsp;&nbsp;&nbsp;&nbsp;If the block must be called, I might expect "i" to be nil.  
&nbsp;&nbsp;&nbsp;&nbsp;What actually happens is that the block is called and "i" is the same  
thing as "a". I did not expect this and I'm trying to figure out how this  
is either consistent or useful. Can someone clue me in to this behaviour?  
&nbsp;&nbsp;&nbsp;&nbsp;Thank you...

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [30 March 2008 06:21 UTC](https://rubytalk.org/t/injects-pathological-case/45508/2 "2008-03-30T06:21:20Z")

</div>

Just Another Victim... wrote:

> I'm not sure how I feel about inject's pathological case. What do you  
> all think should happen in the following code?
> 
> [2].inject {|a, i| puts i}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;I might have thought that the block would never be called and the  
> method  
> would return 2.  
> &nbsp;&nbsp;&nbsp;&nbsp;If the block must be called, I might expect "i" to be nil.

> &nbsp;&nbsp;&nbsp;&nbsp;What actually happens is that the block is called and "i" is the  
> same  
> thing as "a". I did not expect this and I'm trying to figure out how  
> this  
> is either consistent or useful. Can someone clue me in to this  
> behaviour?

I'm not seeing that:

1) Two element array:

result = [2, 3].inject do |a, i|  
&nbsp;&nbsp;puts "a:#{a}"  
&nbsp;&nbsp;puts "i:#{i}"  
&nbsp;&nbsp;puts "hello"  
&nbsp;&nbsp;puts "--------"  
end

puts "result=#{result}"  
if result.nil?  
&nbsp;&nbsp;puts 'yes'  
end

--output:--  
i:3  
hello

> **···**
>
> a:2  
> --------  
> result=  
> yes
> 
> 2) One element array:
> 
> result = [2].inject do |a, i|  
> &nbsp;&nbsp;puts "a:#{a}"  
> &nbsp;&nbsp;puts "i:#{i}"  
> &nbsp;&nbsp;puts "hello"  
> &nbsp;&nbsp;puts "--------"  
> end
> 
> puts "result=#{result}"  
> if result.nil?  
> &nbsp;&nbsp;puts 'yes'  
> end
> 
> --output:--  
> result=2  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 March 2008 07:14 UTC](https://rubytalk.org/t/injects-pathological-case/45508/3 "2008-03-30T07:14:21Z")

</div>

Hi --

> &nbsp;&nbsp;&nbsp;I'm not sure how I feel about inject's pathological case. What do you  
> all think should happen in the following code?
> 
> [2].inject {|a, i| puts i}
> 
> &nbsp;&nbsp;&nbsp;I might have thought that the block would never be called and the method  
> would return 2.  
> &nbsp;&nbsp;&nbsp;If the block must be called, I might expect "i" to be nil.  
> &nbsp;&nbsp;&nbsp;What actually happens is that the block is called and "i" is the same  
> thing as "a". I did not expect this and I'm trying to figure out how this  
> is either consistent or useful. Can someone clue me in to this behaviour?  
> &nbsp;&nbsp;&nbsp;Thank you...

Actually the block isn't being called:

&nbsp;&nbsp;&nbsp;irb(main):005:0\> [2].inject {|a, i| puts "hi" }  
&nbsp;&nbsp;&nbsp;=\> 2  
&nbsp;&nbsp;&nbsp;irb(main):006:0\> [2].inject {}  
&nbsp;&nbsp;&nbsp;=\> 2

The value of the whole statement is 2, or, more generally, the first  
(and only) element in the enumerable. I guess that's all it can do  
since it doesn't have enough elements to call the block even once.

David

> **···**
>
> On Sun, 30 Mar 2008, Just Another Victim of the Ambient Morality wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS April 14-17 New York City  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [30 March 2008 06:27 UTC](https://rubytalk.org/t/injects-pathological-case/45508/4 "2008-03-30T06:27:20Z")

</div>

Oh, yeah:

$ ruby -v  
ruby 1.8.2 (2004-12-25) [universal-darwin8.0]

By the way, using inject() is inefficient--not to mention confusing.  
You might as well pretend it doesn't exist.

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

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [30 March 2008 09:38 UTC](https://rubytalk.org/t/injects-pathological-case/45508/5 "2008-03-30T09:38:43Z")

</div>

David A. Black wrote:

> Actually the block isn't being called:  
> I guess that's all it can do  
> since it doesn't have enough elements to call the block even once.

To add to that, on p.456 of pickaxe2 it says that if you don't supply an  
argument for the inject call, then the first element in the enumerable  
becomes 'a', and it is not included in subsequent iteration.  
Apparently, because there are no values to iterate over, the block does  
not execute.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Just\_Another\_Victim1](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Just\_Another\_Victim1](https://rubytalk.org/u/Just_Another_Victim1)\
**Post date:** [30 March 2008 20:05 UTC](https://rubytalk.org/t/injects-pathological-case/45508/6 "2008-03-30T20:05:08Z")

</div>

"David A. Black" \<dblack@rubypal.com\> wrote in message  
news:Pine.LNX.4.64.0803300208460.27343@rubypal.com...

> Hi --
> 
> > &nbsp;&nbsp;&nbsp;I'm not sure how I feel about inject's pathological case. What do you  
> > all think should happen in the following code?
> > 
> > [2].inject {|a, i| puts i}
> > 
> > &nbsp;&nbsp;&nbsp;I might have thought that the block would never be called and the  
> > method  
> > would return 2.  
> > &nbsp;&nbsp;&nbsp;If the block must be called, I might expect "i" to be nil.  
> > &nbsp;&nbsp;&nbsp;What actually happens is that the block is called and "i" is the same  
> > thing as "a". I did not expect this and I'm trying to figure out how  
> > this  
> > is either consistent or useful. Can someone clue me in to this  
> > behaviour?  
> > &nbsp;&nbsp;&nbsp;Thank you...
> 
> Actually the block isn't being called:
> 
> &nbsp;&nbsp;irb(main):005:0\> [2].inject {|a, i| puts "hi" }  
> &nbsp;&nbsp;=\> 2  
> &nbsp;&nbsp;irb(main):006:0\> [2].inject {}  
> &nbsp;&nbsp;=\> 2
> 
> The value of the whole statement is 2, or, more generally, the first  
> (and only) element in the enumerable. I guess that's all it can do  
> since it doesn't have enough elements to call the block even once.

&nbsp;&nbsp;&nbsp;&nbsp;D'oh!  
&nbsp;&nbsp;&nbsp;&nbsp;I was confused by IRB's interface, for no good reason. I'll put the  
crack pipe down, now. It's doing exactly what I thought it should do...

> **···**
>
> > On Sun, 30 Mar 2008, Just Another Victim of the Ambient Morality wrote:

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://rubytalk.org/u/Thufir)\
**Post date:** [1 April 2008 20:13 UTC](https://rubytalk.org/t/injects-pathological-case/45508/7 "2008-04-01T20:13:09Z")

</div>

[...]

How do you print the value for "a" and "i"?

thufir@arrakis:~/ruby$  
thufir@arrakis:~/ruby$ ruby inject\_one\_element.rb  
result=2  
thufir@arrakis:~/ruby$  
thufir@arrakis:~/ruby$ cat inject\_one\_element.rb  
result = [2].inject do |a, i|  
&nbsp;&nbsp;puts "a:#{a}"  
&nbsp;&nbsp;puts "i:#{i}"  
&nbsp;&nbsp;puts "hello"  
&nbsp;&nbsp;puts "--------"  
end

puts "result=#{result}"  
if result.nil?  
&nbsp;&nbsp;puts 'yes'  
end  
thufir@arrakis:~/ruby$

thanks,

Thufir

> **···**
>
> On Sun, 30 Mar 2008 15:21:20 +0900, 7stud -- wrote:
> 
> > 2) One element array:

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [30 March 2008 09:55 UTC](https://rubytalk.org/t/injects-pathological-case/45508/8 "2008-03-30T09:55:08Z")

</div>

There is some truth to that.

def collect\_repeats\_inject list  
&nbsp;&nbsp;return if == list  
&nbsp;&nbsp;list[1..-1].inject([[list.first]]){|a,e|  
&nbsp;&nbsp;&nbsp;&nbsp;if a[-1][0] == e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[-1] \<\< e  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a \<\< [e]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;a  
&nbsp;&nbsp;}.reject{|lst| lst.size \< 2 }  
end

def collect\_repeats list  
&nbsp;&nbsp;accum = [[ list.shift] ]  
&nbsp;&nbsp;list.each{|e|  
&nbsp;&nbsp;&nbsp;&nbsp;if accum[-1][0] == e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accum[-1] \<\< e  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accum \<\< [e]  
&nbsp;&nbsp;&nbsp;&nbsp;end }  
&nbsp;&nbsp;accum.reject{|lst| lst.size \< 2 }  
end

p collect\_repeats( %w(0 1 1 2 3 3 3 3 4 5 5 6) )  
p collect\_repeats( )  
p collect\_repeats\_inject( %w(0 1 1 2 3 3 3 3 4 5 5 6) )  
p collect\_repeats\_inject( )

the\_list = %w(0 1 1 2 3 3 3 3 3 3 3 3 4 5 5 6 7 8 8 9 9 9)

t = Time.now  
9999.times{ collect\_repeats\_inject( the\_list )}  
p Time.now - t  
t = Time.now  
9999.times{ collect\_repeats( the\_list )}  
p Time.now - t

--- output ---  
[["1", "1"], ["3", "3", "3", "3"], ["5", "5"]]  
  
[["1", "1"], ["3", "3", "3", "3"], ["5", "5"]]  
  
2.694  
0.16

And the version without inject is shorter and  
clearer.

> **···**
>
> On Mar 30, 12:27 am, 7stud -- \<bbxx789\_0...@yahoo.com\> wrote:
> 
> > Oh, yeah:
> > 
> > $ ruby -v  
> > ruby 1.8.2 (2004-12-25) [universal-darwin8.0]
> > 
> > By the way, using inject() is inefficient--not to mention confusing.  
> > You might as well pretend it doesn't exist.

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [30 March 2008 10:05 UTC](https://rubytalk.org/t/injects-pathological-case/45508/9 "2008-03-30T10:05:10Z")

</div>

There is some truth to that.

def collect\_repeats\_inject list  
&nbsp;&nbsp;return if == list  
&nbsp;&nbsp;list[1..-1].inject([[list.first]]){|a,e|  
&nbsp;&nbsp;&nbsp;&nbsp;if a[-1][0] == e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[-1] \<\< e  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a \<\< [e]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;a  
&nbsp;&nbsp;}.reject{|lst| lst.size \< 2 }  
end

def collect\_repeats list  
&nbsp;&nbsp;return if == list  
&nbsp;&nbsp;accum = [[ list.first] ]  
&nbsp;&nbsp;list[1..-1].each{|e|  
&nbsp;&nbsp;&nbsp;&nbsp;if accum[-1][0] == e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accum[-1] \<\< e  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accum \<\< [e]  
&nbsp;&nbsp;&nbsp;&nbsp;end }  
&nbsp;&nbsp;accum.reject{|lst| lst.size \< 2 }  
end

p collect\_repeats( %w(0 1 1 2 3 3 3 3 4 5 5 6) )  
p collect\_repeats( )  
p collect\_repeats\_inject( %w(0 1 1 2 3 3 3 3 4 5 5 6) )  
p collect\_repeats\_inject( )

the\_list = %w(0 1 1 2 3 3 3 3 3 3 3 3 4 5 5 6 7 8 8 9 9 9)

t = Time.now  
9999.times{ collect\_repeats\_inject( the\_list )}  
p Time.now - t  
t = Time.now  
9999.times{ collect\_repeats( the\_list )}  
p Time.now - t

--- output ---  
[["1", "1"], ["3", "3", "3", "3"], ["5", "5"]]  
  
[["1", "1"], ["3", "3", "3", "3"], ["5", "5"]]  
  
2.814  
1.923

And the version without inject seems clearer.

> **···**
>
> On Mar 30, 12:27 am, 7stud -- \<bbxx789\_0...@yahoo.com\> wrote:
> 
> > Oh, yeah:
> > 
> > $ ruby -v  
> > ruby 1.8.2 (2004-12-25) [universal-darwin8.0]
> > 
> > By the way, using inject() is inefficient--not to mention confusing.  
> > You might as well pretend it doesn't exist.

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [30 March 2008 13:34 UTC](https://rubytalk.org/t/injects-pathological-case/45508/10 "2008-03-30T13:34:19Z")

</div>

Or much better, understand that it's a way to reduce/fold the elements  
of an enumerable into a single value.

> **[Fold (higher-order function)](https://en.wikipedia.org/wiki/Fold_%28higher-order_function%29)**
>
> In functional programming, fold (also termed reduce, accumulate, aggregate, compress, or inject) refers to a family of higher-order functions that analyze a recursive data structure and through use of a given combining operation, recombine the results of recursively processing its constituent parts, building up a return value. Typically, a fold is presented with a combining function, a top node of a data structure, and possibly some default values to be used under certain conditions. The fold the...

Enumerable#inject is very useful, as long as you don't treat it as  
Maslow's hammer and use it in inappropriate ways, such as giving it a  
block like in the original posting to this thread.

> **···**
>
> On 3/30/08, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > By the way, using inject() is inefficient--not to mention confusing.  
> > You might as well pretend it doesn't exist.
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [30 March 2008 15:51 UTC](https://rubytalk.org/t/injects-pathological-case/45508/11 "2008-03-30T15:51:42Z")

</div>

I strongly disagree with that statement.

So 7stud, you learned Python and Ruby, decided you prefer Python, and now hang out on the Ruby Talk mailing list bad mouthing our language? What's the point, if you don't mind my asking?

James Edward Gray II

> **···**
>
> On Mar 30, 2008, at 1:27 AM, 7stud -- wrote:
> 
> > By the way, using inject() is inefficient--not to mention confusing.  
> > You might as well pretend it doesn't exist.

---

<div class="post-metadata">

**Author:** ![Thomas\_Wieczorek](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@Thomas\_Wieczorek](https://rubytalk.org/u/Thomas_Wieczorek)\
**Post date:** [30 March 2008 21:41 UTC](https://rubytalk.org/t/injects-pathological-case/45508/12 "2008-03-30T21:41:37Z")

</div>

I absolutely can't agree with that. map and inject are two beautiful  
functions which should be known for the average Ruby programmer imo.  
Instead of using some loops, you can easily use map to apply a  
function on each member. I like them.

> **···**
>
> On Sun, Mar 30, 2008 at 8:27 AM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > Oh, yeah:
> > 
> > $ ruby -v  
> > ruby 1.8.2 (2004-12-25) [universal-darwin8.0]
> > 
> > By the way, using inject() is inefficient--not to mention confusing.  
> > You might as well pretend it doesn't exist.

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://rubytalk.org/u/Thufir)\
**Post date:** [1 April 2008 20:15 UTC](https://rubytalk.org/t/injects-pathological-case/45508/13 "2008-04-01T20:15:04Z")

</div>

What's the "normal" way, not using inject?

-Thufir

> **···**
>
> On Sun, 30 Mar 2008 15:27:20 +0900, 7stud -- wrote:
> 
> > By the way, using inject() is inefficient--not to mention confusing. You  
> > might as well pretend it doesn't exist.

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [30 March 2008 14:30 UTC](https://rubytalk.org/t/injects-pathological-case/45508/14 "2008-03-30T14:30:31Z")

</div>

> Understand that it's a way to reduce/fold the elements  
> of an enumerable into a single value.

Not that this is important, but personally I never liked the name  
.inject  
Though fold(l/r) is not much better either.

A peculiar thing is that the wikipedia page gives "also known variously  
as " four different names as alternative/example.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [30 March 2008 14:32 UTC](https://rubytalk.org/t/injects-pathological-case/45508/15 "2008-03-30T14:32:58Z")

</div>

Completely agree with Rick and furthermore you will not be able to  
understand Ruby code written by others if you do not have a basic  
understanding of inject.

I found this code in Ruby1.9 though

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if result.size \> 0 and result.inject(false) {|k,s| s or k}

that kind of code explains why inject has a bad reputation.

Cheers  
Robert

> **···**
>
> On Sun, Mar 30, 2008 at 3:34 PM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:
> 
> > On 3/30/08, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> > 
> > \> By the way, using inject() is inefficient--not to mention confusing.  
> > \> You might as well pretend it doesn't exist.
> > 
> > Or much better, understand that it's a way to reduce/fold the elements  
> > of an enumerable into a single value.  
> > [Fold (higher-order function) - Wikipedia](http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29)
> > 
> > Enumerable#inject is very useful, as long as you don't treat it as  
> > Maslow's hammer and use it in inappropriate ways, such as giving it a  
> > block like in the original posting to this thread.
> > 
> > --  
> > Rick DeNatale
> > 
> > My blog on Ruby  
> > [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Julian\_Leviston](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Julian\_Leviston](https://rubytalk.org/u/Julian_Leviston)\
**Post date:** [31 March 2008 01:06 UTC](https://rubytalk.org/t/injects-pathological-case/45508/16 "2008-03-31T01:06:59Z")

</div>

They indispensible. Once you understand the other iterators, inject becomes natural.

Try doing some form of aggregation without using inject. Haha fun.

# example program to illustrate inject  
num\_ary = [10,37,27,398,273,28]

# summing without inject:  
without\_inject\_sum = 0  
num\_ary.each{|num| without\_inject\_sum += num}

# summing with inject  
with\_inject\_sum = num\_ary.inject{|sum, num| sum + num}

Julian

> **···**
>
> On 31/03/2008, at 8:41 AM, Thomas Wieczorek wrote:
> 
> > On Sun, Mar 30, 2008 at 8:27 AM, 7stud -- \<bbxx789\_05ss@yahoo.com\> \> wrote:
> > 
> > > Oh, yeah:
> > > 
> > > $ ruby -v  
> > > ruby 1.8.2 (2004-12-25) [universal-darwin8.0]
> > > 
> > > By the way, using inject() is inefficient--not to mention confusing.  
> > > You might as well pretend it doesn't exist.
> > 
> > I absolutely can't agree with that. map and inject are two beautiful  
> > functions which should be known for the average Ruby programmer imo.  
> > Instead of using some loops, you can easily use map to apply a  
> > function on each member. I like them.

---

<div class="post-metadata">

**Author:** ![Julian\_Leviston](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Julian\_Leviston](https://rubytalk.org/u/Julian_Leviston)\
**Post date:** [31 March 2008 03:49 UTC](https://rubytalk.org/t/injects-pathological-case/45508/17 "2008-03-31T03:49:20Z")

</div>

...or you could do it like this:

def collect\_repeats\_inject(list)  
&nbsp;&nbsp;&nbsp;return list unless list.respond\_to?(:inject)  
&nbsp;&nbsp;&nbsp;collected\_hash = list.inject({}) do |collected\_repeats, item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( collected\_hash[item] ||= ) \<\< item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collected\_hash  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;collected\_hash.values  
end

> **···**
>
> On 30/03/2008, at 8:55 PM, William James wrote:
> 
> > On Mar 30, 12:27 am, 7stud -- \<bbxx789\_0...@yahoo.com\> wrote:
> > 
> > > Oh, yeah:
> > > 
> > > $ ruby -v  
> > > ruby 1.8.2 (2004-12-25) [universal-darwin8.0]
> > > 
> > > By the way, using inject() is inefficient--not to mention confusing.  
> > > You might as well pretend it doesn't exist.
> > 
> > There is some truth to that.
> > 
> > def collect\_repeats\_inject list  
> > return if == list  
> > list[1..-1].inject([[list.first]]){|a,e|  
> > &nbsp;&nbsp;&nbsp;if a[-1][0] == e  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[-1] \<\< e  
> > &nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a \<\< [e]  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;a  
> > }.reject{|lst| lst.size \< 2 }  
> > end
> > 
> > def collect\_repeats list  
> > accum = [[list.shift] ]  
> > list.each{|e|  
> > &nbsp;&nbsp;&nbsp;if accum[-1][0] == e  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accum[-1] \<\< e  
> > &nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accum \<\< [e]  
> > &nbsp;&nbsp;&nbsp;end }  
> > accum.reject{|lst| lst.size \< 2 }  
> > end
> > 
> > p collect\_repeats( %w(0 1 1 2 3 3 3 3 4 5 5 6) )  
> > p collect\_repeats( )  
> > p collect\_repeats\_inject( %w(0 1 1 2 3 3 3 3 4 5 5 6) )  
> > p collect\_repeats\_inject( )
> > 
> > the\_list = %w(0 1 1 2 3 3 3 3 3 3 3 3 4 5 5 6 7 8 8 9 9 9)
> > 
> > t = Time.now  
> > 9999.times{ collect\_repeats\_inject( the\_list )}  
> > p Time.now - t  
> > t = Time.now  
> > 9999.times{ collect\_repeats( the\_list )}  
> > p Time.now - t
> > 
> > --- output ---  
> > [["1", "1"], ["3", "3", "3", "3"], ["5", "5"]]  
> >   
> > [["1", "1"], ["3", "3", "3", "3"], ["5", "5"]]  
> >   
> > 2.694  
> > 0.16
> > 
> > And the version without inject is shorter and  
> > clearer.

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://rubytalk.org/u/Thufir)\
**Post date:** [1 April 2008 20:25 UTC](https://rubytalk.org/t/injects-pathological-case/45508/18 "2008-04-01T20:25:04Z")

</div>

Ah, the irony: when googling Maslow's hammer:

Microsoft JET Database Engine error '80040e14'

Syntax error (missing operator) in query expression ''http://  
www.google.ca/search?q=Maslow’s+hamme

> **[Engaging Essay Topics on Hierarchy of Needs by Abraham Maslow 🤓| Studyfy.com](https://studyfy.com/writing-guides/best-essay-topic-abraham-maslows)**
>
> The world knows Abraham Maslow as the author of the pyramid of needs, from the simplest to the highest ones. Abraham Maslow’s Hierarchy of Needs essay topics are relevant for those willing to delve into psychological sciences and comprehend the...

ROFL,

Thufir

> **···**
>
> On Sun, 30 Mar 2008 22:34:19 +0900, Rick DeNatale wrote:
> 
> > Enumerable#inject is very useful, as long as you don't treat it as  
> > Maslow's hammer and use it in inappropriate ways, such as giving it a  
> > block like in the original posting to this thread.

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [2 April 2008 11:55 UTC](https://rubytalk.org/t/injects-pathological-case/45508/19 "2008-04-02T11:55:04Z")

</div>

The same as for any troll?

> **···**
>
> On Mar 30, 11:51 am, James Gray \<ja...@grayproductions.net\> wrote:
> 
> > On Mar 30, 2008, at 1:27 AM, 7stud -- wrote:
> > 
> > \> By the way, using inject() is inefficient--not to mention confusing.  
> > \> You might as well pretend it doesn't exist.
> > 
> > I strongly disagree with that statement.
> > 
> > So 7stud, you learned Python and Ruby, decided you prefer Python, and  
> > now hang out on the Ruby Talk mailing list bad mouthing our language?  
> > What's the point, if you don't mind my asking?

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [30 March 2008 15:17 UTC](https://rubytalk.org/t/injects-pathological-case/45508/20 "2008-03-30T15:17:04Z")

</div>

It's because the same thing has surfaced in several languages using  
different terms, and those languages have in turn influenced others.

AFAIK, Matz correct me if I'm wrong, Ruby got the name inject from  
Smalltalk, along with collect, select, detect and several other  
methods. The other three I mention have alias, map for collect,  
find\_all for select, and find for detect, but inject is still just  
inject in Ruby.

> **···**
>
> On Sun, Mar 30, 2008 at 10:30 AM, Marc Heiler \<shevegen@linuxmail.org\> wrote:
> 
> > \> Understand that it's a way to reduce/fold the elements  
> > \> of an enumerable into a single value.
> > 
> > Not that this is important, but personally I never liked the name  
> > .inject  
> > Though fold(l/r) is not much better either.
> > 
> > A peculiar thing is that the wikipedia page gives "also known variously  
> > as " four different names as alternative/example.
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

[Next page](https://rubytalk.org/t/injects-pathological-case/45508.md?page=2)
