From: Reuben Grinberg Date: 2007-04-10T02:00:09+09:00 Subject: Re: Creating an array of differences Here's another way of doing it: (a.zip(a[1..a.length])[0...a.length-1]).map { |a,b| b-a } This things makes an array of pairs of the elements: irb(main):007:0> a = [1000, 1100, 1200] => [1000, 1100, 1200] irb(main):029:0> a.zip(a[1..a.length])[0...a.length-1] => [[1000, 1100], [1100, 1200]] irb(main):030:0> (a.zip(a[1..a.length])[0...a.length-1]).map { |a,b| b-a } => [100, 100] -Reuben Jason Madigan wrote: > Hey there, > > I'm pretty new to Ruby so perhaps some of you could help me out with a > little problem I've come up against. I have an array consisting of > integers (called session[:octets_in]). What I want to do is create an > array comprising of the difference between the current element and the > previous element (e.g. ["1000", "1100", "1200"] would become ["100", > "200"]). I'm not entirely sure how to go about this, so any pointers > would be welcome. >