On Wed, Jul 29, 2009 at 8:18 AM, Harry Kakueki<list.push / gmail.com> wrote:
>>
>> 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.
>>
>>
>
> But, you are doing the opposite :)
> Anyway, based on your code,
>
> FWIW, here is another one-liner.
> Elegant? I don't know.
>
> b = Array.new(a.length){|i| a[i]-a[i-1]}[1..-1].unshift(a[0])

Another way:

irb(main):001:0> a = [10,20,30,40]
irb(main):014:0> b = []
=> []
irb(main):015:0> a.each_with_index {|x,y| b << (y > 0? (x - a[y-1]): x)}
=> [10, 20, 30, 40]

irb(main):019:0> b
=> [10, 10, 10, 10]

Jesus.