On Jul 28, 10:39 pm, Jason Lillywhite <jason.lillywh... / gmail.com> wrote: > Is this a good way to use a previous value in an array block? > > I want to subtract the item(i) from item(i-1) of the array, a and return > a new array of the results. Below is my attempt. > > a = [1,3,3,2] > prev_item = 0 > b = [] > > a.each do |item| > b << item - prev_item > prev_item = item > end > > puts b > > => 1,2,0,-1 > > I'm thinking there must be a more elegant way of doing this. > > thank you! > -- > Posted viahttp://www.ruby-forum.com/. Never forget: we don't need no stinkin' loops! x = 2,3,5,8,13 p x.zip( [0] + x ).map{|a,b| a-b} => [2, 1, 2, 3, 5]