Class: Timer

Inherits:
Object show all
Defined in:
lib/carat/timer.rb,
lib/carat-dev/timer/timer.rb

Overview

Ruby license. Copyright ©2004 Joel VanderWerf. Contact [email protected].

A lightweight, non-drifting, self-correcting timer. Average error is bounded as long as, on average, there is enough time to complete the work done, and the timer is checked often enough. It is lightweight in the sense that no threads are created. Can be used either as an internal iterator (Timer.every) or as an external iterator (Timer.new).

Simple usage:

require ‘timer’

Timer.every(0.1, 0.5) { |elapsed| puts elapsed }

timer = Timer.new(0.1) 5.times do

puts timer.elapsed
timer.wait

end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period = 1) ⇒ Timer

Make a Timer that can be checked when needed, using #wait or #if_ready. The advantage over Timer.every is that the timer can be checked on separate passes through a loop.



41
42
43
44
45
46
47
# File 'lib/carat-dev/timer/timer.rb', line 41

def initialize
  @start_time = nil
  @end_time = nil
  @total_time = 0
  @runnning = nil
  start
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



52
53
54
# File 'lib/carat/timer.rb', line 52

def end_time
  @end_time
end

#periodObject (readonly)

Returns the value of attribute period.



46
47
48
# File 'lib/carat-dev/timer/timer.rb', line 46

def period
  @period
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



51
52
53
# File 'lib/carat/timer.rb', line 51

def start_time
  @start_time
end

Class Method Details

.every(period, expire = nil) ⇒ Object

Yields to the supplied block every period seconds. The value yielded is the total elapsed time (an instance of Time). If expire is given, then #every returns after that amount of elapsed time.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/carat-dev/timer/timer.rb', line 26

def Timer.every(period, expire = nil)
  target = time_start = Time.now
  loop do
    elapsed = Time.now - time_start
    break if expire and elapsed > expire
    yield elapsed
    target += period
    error = target - Time.now
    sleep error if error > 0
  end
end

Instance Method Details

#elapsedObject

Time on timer since instantiation or last #restart.



55
56
57
# File 'lib/carat-dev/timer/timer.rb', line 55

def elapsed
  Time.now - @time_start
end

#if_readyObject

Yield to the block if no time remains in cycle. Otherwise, return immediately to caller



70
71
72
73
74
75
76
77
# File 'lib/carat-dev/timer/timer.rb', line 70

def if_ready
  error = @target + @period - Time.now
  if error <= 0
    @target += @period
    elapsed = Time.now - @time_start
    yield elapsed
  end
end

#restartObject

Call this to restart the timer after a period of inactivity (e.g., the user hits the pause button, and then hits the go button).



50
51
52
# File 'lib/carat-dev/timer/timer.rb', line 50

def restart
  @target = @time_start = Time.now
end

#running?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/carat/timer.rb', line 73

def running?
  return @running
end

#startObject



62
63
64
65
# File 'lib/carat/timer.rb', line 62

def start
  @start_time = Time.now
  @running = true
end

#stopObject



67
68
69
70
71
# File 'lib/carat/timer.rb', line 67

def stop
  @end_time = Time.now
  @total_time += @end_time - @start_time
  @running = false
end

#stopped?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/carat/timer.rb', line 77

def stopped?
  return !@running
end

#total_timeObject



81
82
83
84
85
86
87
# File 'lib/carat/timer.rb', line 81

def total_time
  if running? then
    return @total_time + (Time.now - @start_time)
  else
    return @total_time
  end
end

#waitObject

Wait for the next cycle, if time remains in the current cycle. Otherwise, return immediately to caller.



61
62
63
64
65
66
# File 'lib/carat-dev/timer/timer.rb', line 61

def wait
  @target += @period
  error = @target - Time.now
  sleep error if error > 0
  true
end