class Timer

A simple timer that counts in seconds.

Usage:

timer = Timer.new
timer.start
...
if timer.delta > 0.5 # half a second
    ...

Public Class Methods

new() click to toggle source

Creates the timer, doing nothing else.

# File lib/tabscroll/timer.rb, line 13
def initialize
  @is_running = false
  @is_paused  = false
end

Public Instance Methods

delta() click to toggle source

Returns the current delta in seconds (float).

# File lib/tabscroll/timer.rb, line 68
def delta
  if @is_running
    return (Time.now.to_f - @start_time.to_f)
  end

  return @paused_time.to_f if @is_paused

  return @start_time if @start_time == 0 # Something's wrong

  return (@stop_time.to_f - @start_time.to_f)
end
pause() click to toggle source
# File lib/tabscroll/timer.rb, line 43
def pause
  return if not @is_running or @is_paused

  @paused_time = (Time.now - @start_time)
  @is_running  = false
  @is_paused   = true
end
paused?() click to toggle source
# File lib/tabscroll/timer.rb, line 63
def paused?
  @is_paused
end
restart() click to toggle source
# File lib/tabscroll/timer.rb, line 38
def restart
  self.stop
  self.start
end
running?() click to toggle source
# File lib/tabscroll/timer.rb, line 59
def running?
  @is_running
end
start() click to toggle source

Starts counting.

# File lib/tabscroll/timer.rb, line 19
def start
  return if @is_running

  @start_time  = Time.now
  @stop_time   = 0.0
  @paused_time = 0.0
  @is_running  = true
  @is_paused   = false
end
stop() click to toggle source

Stops counting.

# File lib/tabscroll/timer.rb, line 30
def stop
  return if not @is_running

  @stop_time  = Time.now
  @is_running = false
  @is_paused  = false
end
to_s() click to toggle source

Converts the timer's delta to a formatted string.

# File lib/tabscroll/timer.rb, line 81
def to_s
  min  = (self.delta / 60).to_i
  sec  = (self.delta).to_i
  msec = (self.delta * 100).to_i

  "#{min}:#{sec}:#{msec}"
end
unpause() click to toggle source
# File lib/tabscroll/timer.rb, line 51
def unpause
  return if not @is_paused or @is_running

  @start_time = (Time.now - @paused_time)
  @is_running = true
  @is_paused  = false
end