Created
November 27, 2013 19:45
-
-
Save michaelminter/7681994 to your computer and use it in GitHub Desktop.
Get word duration from time in seconds Extend Numeric so that you can invoke the method on any numeric class (Fixnum, Bignum, or in your case Float).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Numeric | |
def duration | |
secs = self.to_int | |
mins = secs / 60 | |
hours = mins / 60 | |
days = hours / 24 | |
if days > 0 | |
"#{days} days and #{hours % 24} hours" | |
elsif hours > 0 | |
"#{hours} hours and #{mins % 60} minutes" | |
elsif mins > 0 | |
"#{mins} minutes and #{secs % 60} seconds" | |
elsif secs >= 0 | |
"#{secs} seconds" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment