ruby - Loop through an array while summing the output -


i trying use for loop , sum of outputs. know basic question, not sure if should trying solve within loop or using array.

for example:

for in 1..100 if foo / 2 == 0 #find sum of outputs end 

end

try this:

(2..100).step(2).inject(:+) #=> 2550 

or:

2.step(100,2).inject(:+) #=> 2550 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -