P
Peña, Botp
Hi All,
inject is a powerful method in ruby. but the ff gives me surprise..
irb(main):001:0> sum=3D0
=3D> 0
irb(main):002:0> [1,2,3,4,5].inject{|sum,e| sum+e }
=3D> 15
irb(main):003:0> sum
=3D> 10
irb(main):004:0>
irb(main):006:0> RUBY_VERSION
=3D> "1.8.5"
irb(main):007:0> prod=3D1
=3D> 1
irb(main):008:0> [1,2,3,4,5].inject(1){|prod,e| prod*e }
=3D> 120
irb(main):009:0> prod
=3D> 24
it seems that inject does not update accumulator (sum or prod in =
examples) on last iteration.
tested on windows and linux.
kind regards
inject is a powerful method in ruby. but the ff gives me surprise..
irb(main):001:0> sum=3D0
=3D> 0
irb(main):002:0> [1,2,3,4,5].inject{|sum,e| sum+e }
=3D> 15
irb(main):003:0> sum
=3D> 10
irb(main):004:0>
irb(main):006:0> RUBY_VERSION
=3D> "1.8.5"
irb(main):007:0> prod=3D1
=3D> 1
irb(main):008:0> [1,2,3,4,5].inject(1){|prod,e| prod*e }
=3D> 120
irb(main):009:0> prod
=3D> 24
it seems that inject does not update accumulator (sum or prod in =
examples) on last iteration.
tested on windows and linux.
kind regards