Monday, October 15, 2007

to_stopwatch

Recent code I used in 2 projects. Useful but not enough to be put in active_core.

Usage
20.to_stopwatch # 00:00:20
class Numeric
def to_stopwatch
hours, remainder = (self / 3660).floor.to_i, self % 3660
minutes, seconds = (remainder / 60).floor.to_i, (remainder % 60).to_i
"#{sprintf("%02d", hours)}:#{sprintf("%02d", minutes)}:#{sprintf("%02d", seconds)}"
end
end

class NilClass
delegate :to_stopwatch, :to => 0
end

No comments: