What is the best class to represent a duration time in Ruby?
I’m writing a Run model that have distance and duration attributes.
If I want to work with minutes and seconds, the most indicated class would be time. However it has not just minutes and seconds, but also day, month, etc. What would be the proper way to store time duration in Ruby?
I’ll need to do calculations with it later, e.g., average pace = duration/distance, and the result should be in the minutes format, i.e., 5’30”, and not 5.50 min.
I use something like this to calculate how long a
Resquejob runs:In this example, I’m going out to hundredths of a second, but you could tweak that if needed and possibly get rid of the
sprintfall together if you wanted. That would leave you with basicallyseconds = (finish - start) % 60Then a simpleputs #{minutes}'#{seconds}"would get you the output you want.