Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created February 8, 2019 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/98100528595a6ddbe9d8a7d31ca9a15e to your computer and use it in GitHub Desktop.
Save havenwood/98100528595a6ddbe9d8a7d31ca9a15e to your computer and use it in GitHub Desktop.
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