From: Michael Edgar Date: 2011-03-03T11:21:18+09:00 Subject: Re: What is inject doing here? 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/ 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 >