From: Adam Prescott Date: 2011-03-03T19:42:02+09:00 Subject: Re: What is inject doing here? --90e6ba1819dae4d954049d91adbb Content-Type: text/plain; charset=UTF-8 On Thu, Mar 3, 2011 at 2:15 AM, Todd Benson 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 puts 1 else puts -1 end end --90e6ba1819dae4d954049d91adbb--