-
-
Save havenwood/98100528595a6ddbe9d8a7d31ca9a15e to your computer and use it in GitHub Desktop.
This file contains 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 Timer | |
def initialize(seconds = 0) | |
@seconds = seconds | |
@minutes = 0 | |
@hours = 0 | |
calculate | |
end | |
attr_reader :seconds, :minutes, :hours | |
def time_string | |
padding(@hours.to_s) + ":" + padding(@minutes.to_s)+ ":" + padding(@seconds.to_s) | |
end | |
def padding(string) | |
return "0" + string if string.length < 2 | |
string | |
end | |
private | |
def calculate | |
while @seconds > 60 | |
@minutes = @minutes + 1 | |
@seconds = @seconds - 60 | |
end | |
while @minutes > 60 | |
@hours = @hours + 1 | |
@minutes = @minutes - 60 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment