J
Jason Lillywhite
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!
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!