Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:28:07+00:00 2026-06-04T06:28:07+00:00

What is the best class to represent a duration time in Ruby? I’m writing

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:28:08+00:00Added an answer on June 4, 2026 at 6:28 am

    I use something like this to calculate how long a Resque job runs:

    start = Time.now
    #code goes here
    finish = Time.now
    seconds = sprintf("%0.02f", (finish - start) % 60)
    minutes = (((finish - start) / 60) % 60).to_i
    

    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 sprintf all together if you wanted. That would leave you with basically seconds = (finish - start) % 60 Then a simple puts #{minutes}'#{seconds}" would get you the output you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Where is the best location to put a plain old Ruby class in a
Best to describe this in code... I have this public class A<T> { public
What is the best way to autoload abstract classes I have a class Formprocessor_Userregistrate
I'm trying to expose a model class via WCF Data Services that has an
On a Rails application I'm working on, I have a model Type that relates
Suppose we have an object which represent a box. class Box { public: int
I have a group of values that represent a state (ON, OFF, READY, ...).
Let's say I have a table that represents a super class, students . And
In my app I have 5 String arrays that represent different fields of objects.
I want to find out the best way of creating time based events in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.