Class: Cabin::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/cabin/timer.rb

Overview

A simple timer class for timing events like a stop watch. Normally you don’t invoke this yourself, but you are welcome to do so.

See also: Cabin::Channel#time

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Timer

Returns a new instance of Timer.



8
9
10
11
# File 'lib/cabin/timer.rb', line 8

def initialize(&block)
  @start = Time.now
  @callback = block if block_given?
end

Instance Method Details

#stopObject

Stop the clock and call the callback with the duration. Also returns the duration of this timer.



15
16
17
18
19
# File 'lib/cabin/timer.rb', line 15

def stop
  duration = Time.now - @start
  @callback.call(duration) if @callback
  return duration
end