My goal is to create an array on integers, each representing the days elapsed between two dates. Eventually I’ll average and do other operations on it.
I’ve reached working code:
require 'date'
dates = ['2020-01-30', '2020-01-24', '2020-01-16'].map { |d| Date.parse(d) }
day_difference = []
dates.each_index do |index|
begin
day_difference.push((dates[index] - dates[index + 1]).to_i)
rescue TypeError # end of array
break
end
end
But I’m wondering if there’s a cleaner way, without having to look out for the last index value. Ruby arrays have a lot of methods, so I wouldn’t be surprised if one of those held a better solution.