Let's say I have an array
array = [1,2,3,4,5]
How do I compare the first with the second value, the second with the third etc.
The only thing I could come up with is this (which is rather ugly)
compared = array.each_with_index.map do |a,i|
array[i+1].nil? ? nil : array[i] - array[i + 1]
end
compared.compact # to remove the last nil value
What I want is
[-1, -1, -1, -1]
Is there a nice "ruby way" of achieving this? without using all the ugly array[i] and array[i+1] stuff.