Skip to content

Instantly share code, notes, and snippets.

@phlipper
Created February 12, 2011 21:19
Show Gist options
  • Save phlipper/824133 to your computer and use it in GitHub Desktop.
Save phlipper/824133 to your computer and use it in GitHub Desktop.
class Timer
attr_accessor :seconds
def initialize(value = 0)
@seconds = value
end
def to_s
time_left = @seconds
# hours
hh = (time_left / 3600).floor.to_s.rjust(2, "0")
# minutes
time_left = time_left % 3600
mm = (time_left / 60).floor.to_s.rjust(2, "0")
# time_left should be remaining seconds
ss = (time_left % 60).to_s.rjust(2, "0")
"#{hh}|#{mm}|#{ss}"
end
end
require 'timer'
require 'rubygems'
require 'rspec'
describe Timer do
before(:each) do
@timer = Timer.new
end
it "should initialize to 0 seconds" do
@timer.seconds.should == 0
end
it "should display 0 seconds as 00|00|00" do
@timer.seconds = 0
@timer.to_s.should == "00|00|00"
end
it "should display 12 seconds as 00|00|12" do
@timer.seconds = 12
@timer.to_s.should == "00|00|12"
end
it "should display 66 seconds as 00|01|06" do
@timer.seconds = 66
@timer.to_s.should == "00|01|06"
end
it "should display 4000 seconds as 01|06|40" do
@timer.seconds = 4000
@timer.to_s.should == "01|06|40"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment