Array element additions in ruby -


i have array:

a1 = [1,2,3,4] 

i want generate array a1:

a2 = [3, 5, 7] 

the formula [a1[0] + a1[1], a1[1] + a1[2], ...].

what ruby way this?

yes, can below:

a1 = [1,2,3,4] a2 = a1.each_cons(2).map{ |a| a.inject(:+) } #=> [3, 5, 7]  

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Socket.connect doesn't throw exception in Android -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -