Class: Timer
Direct Known Subclasses
Defined Under Namespace
Classes: Dummy
Instance Attribute Summary collapse
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#time_limit ⇒ Object
Returns the value of attribute time_limit.
Instance Method Summary collapse
-
#defuse ⇒ Object
kill time limit thread, if any.
-
#initialize(time_limit = nil, &block) ⇒ Timer
constructor
A new instance of Timer.
-
#limit(time_limit = nil) ⇒ Object
establish a timelimit on execution.
- #on_timeout(&block) ⇒ Object
-
#reset ⇒ Object
Stops and resets the timer.
-
#resetlimit ⇒ Object
Resets the time limit.
-
#running? ⇒ Boolean
Queries whether the timer is still running.
- #start ⇒ Object
-
#stop ⇒ Object
Stops timer and returns total time.
-
#stopped? ⇒ Boolean
Queries whether the timer is still not running.
-
#total_time ⇒ Object
Queries total recorded time of timer.
Constructor Details
#initialize(time_limit = nil, &block) ⇒ Timer
Returns a new instance of Timer.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mega/timer.rb', line 81 def initialize( time_limit=nil, &block ) # standard timer @start_time = nil @end_time = nil @total_time = 0 @runnning = nil # for using time limit @time_limit = time_limit @on_timeout = block @current_thread = nil @timer_thread = nil end |
Instance Attribute Details
#end_time ⇒ Object (readonly)
Returns the value of attribute end_time.
78 79 80 |
# File 'lib/mega/timer.rb', line 78 def end_time @end_time end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
78 79 80 |
# File 'lib/mega/timer.rb', line 78 def start_time @start_time end |
#time_limit ⇒ Object
Returns the value of attribute time_limit.
79 80 81 |
# File 'lib/mega/timer.rb', line 79 def time_limit @time_limit end |
Instance Method Details
#defuse ⇒ Object
kill time limit thread, if any
138 139 140 141 142 143 |
# File 'lib/mega/timer.rb', line 138 def defuse if @timer_thread Thread.kill @timer_thread @timer_thread = nil end end |
#limit(time_limit = nil) ⇒ Object
establish a timelimit on execution
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/mega/timer.rb', line 123 def limit( time_limit=nil ) if @time_limit || time_limit @current_thread = Thread.current @timer_thread = Thread.fork { sleep @time_limit if @on_timeout then @on_timeout.call @time_limit else @current_thread.raise TimeoutError, "#{@time_limit} seconds past" end } end end |
#on_timeout(&block) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/mega/timer.rb', line 94 def on_timeout( &block ) if block then @on_timeout = block true else false end end |
#reset ⇒ Object
Stops and resets the timer. If the timer was running returns the total time. If not returns 0.
161 162 163 164 165 166 167 168 169 |
# File 'lib/mega/timer.rb', line 161 def reset if running? r = stop else r = 0 end @total_time = 0 return r end |
#resetlimit ⇒ Object
Resets the time limit. Same as:
t.stop
t.start
176 177 178 179 180 181 |
# File 'lib/mega/timer.rb', line 176 def resetlimit #stop #start defuse limit end |
#running? ⇒ Boolean
Queries whether the timer is still running.
184 185 186 |
# File 'lib/mega/timer.rb', line 184 def running? return @running end |
#start ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mega/timer.rb', line 103 def start @running = true @start_time = Time.now limit if @time_limit self #if block_given? then # begin # yield( self ) # ensure # stop # end #else # @time_limit #end end |
#stop ⇒ Object
Stops timer and returns total time. If timer was not running returns false.
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/mega/timer.rb', line 147 def stop if @running defuse # record running time @end_time = Time.now @running = false @total_time += (@end_time - @start_time) else nil end end |
#stopped? ⇒ Boolean
Queries whether the timer is still not running.
189 190 191 |
# File 'lib/mega/timer.rb', line 189 def stopped? return !@running end |