# What is inject doing here?

**URL:** <https://rubytalk.org/t/what-is-inject-doing-here/62006>\
**Category:** ruby-talk\
**Created:** [3 March 2011 02:15 UTC](https://rubytalk.org/t/what-is-inject-doing-here/62006 "2011-03-03T02:15:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [3 March 2011 02:15 UTC](https://rubytalk.org/t/what-is-inject-doing-here/62006/1 "2011-03-03T02:15:44Z")

</div>

Why does this not do what I expect?

irb(main):001:0\> RUBY\_VERSION  
=\> "1.9.2"  
irb(main):002:0\> (0..3).inject {|s, i| a = i%2 == 0 ? 1 : -1; p a}  
-1  
1  
-1  
=\> -1

I would think the result should be

1  
-1  
1  
-1  
=\> -1

Todd

---

<div class="post-metadata">

**Author:** ![Michael\_Edgar](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Michael\_Edgar](https://rubytalk.org/u/Michael_Edgar)\
**Post date:** [3 March 2011 02:21 UTC](https://rubytalk.org/t/what-is-inject-doing-here/62006/2 "2011-03-03T02:21:18Z")

</div>

The first two elements of the range are the first two elements to be used as arguments to  
the inject block:

First, s = 0, i = 1:

a = 1 % 2 == 0 ? 1 : -1; p a # a = -1

Then, s = -1, i = 2:

a = -1 % 2 == 0 ? 1 : -1; p a # a = 1

Then, s = 1, i = 3:

a = 3 % 2 == 0 ? 1 : -1; p a # a = -1

And the range is exhausted.

Michael Edgar  
adgar@carboni.ca  
[http://carboni.ca/](http://carboni.ca/)

> **···**
>
> On Mar 2, 2011, at 9:15 PM, Todd Benson wrote:
> 
> > Why does this not do what I expect?
> > 
> > irb(main):001:0\> RUBY\_VERSION  
> > =\> "1.9.2"  
> > irb(main):002:0\> (0..3).inject {|s, i| a = i%2 == 0 ? 1 : -1; p a}  
> > -1  
> > 1  
> > -1  
> > =\> -1
> > 
> > I would think the result should be
> > 
> > 1  
> > -1  
> > 1  
> > -1  
> > =\> -1
> > 
> > Todd

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott1](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Adam\_Prescott1](https://rubytalk.org/u/Adam_Prescott1)\
**Post date:** [3 March 2011 10:42 UTC](https://rubytalk.org/t/what-is-inject-doing-here/62006/3 "2011-03-03T10:42:02Z")

</div>

Why is this done using inject? s is never used in the block, but since `p a`  
returns nil, s would be nil in every iteration after the first one.

(0..3).map { |n| n % 2 == 0 ? 1 : -1 }.each { |n| puts n }

You could of course just use each by itself:

(0..3).each do |n|  
&nbsp;&nbsp;if n % 2 == 0  
&nbsp;&nbsp;&nbsp;&nbsp;puts 1  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;puts -1  
&nbsp;&nbsp;end  
end

> **···**
>
> On Thu, Mar 3, 2011 at 2:15 AM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > irb(main):002:0\> (0..3).inject {|s, i| a = i%2 == 0 ? 1 : -1; p a}  
> > -1  
> > 1  
> > -1  
> > =\> -1

---

<div class="post-metadata">

**Author:** ![Michael\_Edgar](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Michael\_Edgar](https://rubytalk.org/u/Michael_Edgar)\
**Post date:** [3 March 2011 17:46 UTC](https://rubytalk.org/t/what-is-inject-doing-here/62006/4 "2011-03-03T17:46:20Z")

</div>

I'm pretty sure he's just trying to understand inject's behavior on ranges,  
not looking for refactoring advice.

Also, on Ruby 1.9, Kernel#p returns its arguments.

Michael Edgar  
adgar@carboni.ca  
[http://carboni.ca/](http://carboni.ca/)

> **···**
>
> On Mar 3, 2011, at 5:42 AM, Adam Prescott wrote:
> 
> > On Thu, Mar 3, 2011 at 2:15 AM, Todd Benson \<caduceass@gmail.com\> wrote:
> > 
> > > irb(main):002:0\> (0..3).inject {|s, i| a = i%2 == 0 ? 1 : -1; p a}  
> > > -1  
> > > 1  
> > > -1  
> > > =\> -1
> > 
> > Why is this done using inject? s is never used in the block, but since `p a`  
> > returns nil, s would be nil in every iteration after the first one.
> > 
> > (0..3).map { |n| n % 2 == 0 ? 1 : -1 }.each { |n| puts n }
> > 
> > You could of course just use each by itself:
> > 
> > (0..3).each do |n|  
> > if n % 2 == 0  
> > &nbsp;&nbsp;&nbsp;puts 1  
> > else  
> > &nbsp;&nbsp;&nbsp;puts -1  
> > end  
> > end

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott1](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Adam\_Prescott1](https://rubytalk.org/u/Adam_Prescott1)\
**Post date:** [3 March 2011 18:11 UTC](https://rubytalk.org/t/what-is-inject-doing-here/62006/5 "2011-03-03T18:11:42Z")

</div>

I forgot about that! 🙂 I caught that one recently courtesy of JEG2.

> **···**
>
> On Thu, Mar 3, 2011 at 5:46 PM, Michael Edgar \<adgar@carboni.ca\> wrote:
> 
> > Also, on Ruby 1.9, Kernel#p returns its arguments.
