I'm writing a Rails app that will allow users to input what time of day they finish a task.
I am storing the times as Time
objects, and I need a way to get the average time of day the task was completed for a user.
How can I accomplish this?
My attempt with Малъ Скрылевъ's answer
@deliveries = Delivery.all
times = Array.new
@deliveries.each do |d|
times.push(d.time)
end
puts times # gives me [2000-01-01 06:15:00 UTC, 2000-01-01 07:18:00 UTC]
unless times.empty?
@avg = Time.at(times.map(&:to_i).reduce(:+) / times.size)
end
puts @avg #gives me 1999-12-31 23:46:30 -0700