File: timer.rb

package info (click to toggle)
ruby-cabin 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: ruby: 1,306; makefile: 12
file content (20 lines) | stat: -rw-r--r-- 575 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require "cabin/namespace"

# 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
class Cabin::Timer
  def initialize(&block)
    @start = Time.now
    @callback = block if block_given?
  end # def initialize

  # Stop the clock and call the callback with the duration.
  # Also returns the duration of this timer.
  def stop
    duration = Time.now - @start
    @callback.call(duration) if @callback
    return duration
  end # def stop
end # class Cabin::Timer