How can I create a new array for summing array elements in place in Ruby?
[1,2,3,4,5].each_cons(2).map {|a, b| a + b }
gives me [3, 5, 7,
[3, 5, 7,
It's not very elegant, but it seems to work :
[1,2,3,4,5].each_with_object([]){|i,l| l<<(l.last||0)+i} #=> [1,3,6,10,15]