On Wed, Jul 29, 2009 at 11:39 AM, Jason
Lillywhite<jason.lillywhite / 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|
>  ¨Â ¼¼ éôåí ðòåößéôå>  ¨Âòåößéôåí éôåí
> end
>
> puts b
>
> => 1,2,0,-1
>
> I'm thinking there must be a more elegant way of doing this.
>

to me, your code is elegant enough.
not sure if making code shorter would make it more, so..

> a = [1,3,3,2]
=> [1, 3, 3, 2]
>  a.zip(a.dup.unshift(0)).map{|x,y| x-y}
=> [1, 2, 0, -1]

kind regards -botp