Skip to content

Instantly share code, notes, and snippets.

@kennyt
Created October 27, 2012 09:44
Show Gist options
  • Save kennyt/3963800 to your computer and use it in GitHub Desktop.
Save kennyt/3963800 to your computer and use it in GitHub Desktop.
10_timer
class Timer
def initialize
@seconds = 0
end
def seconds= (seconds)
@seconds = seconds
end
def seconds
@seconds
end
def padded (number)
number.to_s.length == 1 ? number = (number.to_s<<"0").reverse : number.to_s
end
def time_string
seconds_string = (((@seconds.to_f / 60) - (@seconds / 60)) * 60).to_i
while seconds_string > 59 do
seconds = seconds - 60
end
seconds_string = padded(seconds_string)
minute_string = (@seconds / 60)
while minute_string > 59 do
minute_string = minute_string - 60
end
minute_string = padded(minute_string)
hour_string = (@seconds / 3600)
while hour_string > 59 do
hour_string = hour_string - 60
end
hour_string = padded(hour_string)
time_string = "#{hour_string}:#{minute_string}:#{seconds_string}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment