Class | Timer
In: | lib/supplemental/facets/timer.rb
Parent: | Object

# Timer

Provides a strightforward means for controlling time critical execution. Can be used as a "stop watch" timer or as a "time bomb" timer:

      t = Timer.new(10) { raise TimeoutError, "timeout!" }
      t.start
        :      # done within 10sec timeout
      t.stop
      t.start
        :
      if condition then
        t.reset       #--> restart timer
      end

A class method is also provided for easily timing the exectuion of a block.

      Timer.time do |timer|
       timer.total_time.round  #=> 0

       sleep 1
       timer.total_time.round  #=> 1

       timer.stop
       timer.total_time.round  #=> 1

       sleep 1
       timer.total_time.round  #=> 1

       timer.start
       timer.total_time.round  #=> 1

       sleep 1
       timer.total_time.round  #=> 2
      end

Thanks to Paul Brannan for TimeLimit and Minero Aoki for Timer. These two libraries served as models for building this class.

### Methods

defuse end_time limit new on_timeout reset reset_limit running? start start_time stop stopped? time total_time

### Classes and Modules

Class Timer::Dummy

### Attributes

time_limit | [RW] |

### Public Class methods

new(time_limit=nil, &block)
time() {|timer = Timer.new.start| ...}

Takes a block and returns the total time it took to execute.

### Public Instance methods

defuse()

Kill time limit thread, if any.

end_time()
limit( time_limit=nil )

Establish a time limit on execution.

on_timeout( &block )
reset()

Stops and resets the timer. If the timer was running returns the total time. If not returns 0.

reset_limit()

Resets the time limit. Same as:

      t.stop
      t.start

running?()

Queries whether the timer is still running.

start()

Start the timer.

start_time()
stop()

Stops timer and returns total time. If timer was not running returns false.

stopped?()

Queries whether the timer is still not running.

total_time()

Queries total recorded time of timer.

[Validate]