From: Jason Roelofs Date: 2008-04-26T00:37:43+09:00 Subject: Re: Array inject function problem On Fri, Apr 25, 2008 at 11:32 AM, Inbroker Adams wrote: > Hello Rubyists, > I am new to the language and the community. > My first problem is this : > > i used the following code : > > sum = 0 > [1,3,5].inject() {|sum, element| sum + element} > print sum > > > I expected to get 9 > but instead i get 4 > > I just installed the Ruby 1.8.6 one-click installer on windows > and wrote the above commands in SciTE. > Any suggestions?? You're confused with how variables work in Ruby puts [1,3,5].inject() {|sum, element| sum + element} # => 9 'sum' in the block is a different scope from your outside 'sum' Jason