Parent

Namespace

Included Modules

Class/Module Index [+]

Quicksearch

Timers

Low precision timers implemented in pure Ruby

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/timers.rb, line 11
def initialize
  @timers = SortedSet.new
end

Public Instance Methods

add(timer) click to toggle source
# File lib/timers.rb, line 59
def add(timer)
  raise TypeError, "not a Timers::Timer" unless timer.is_a? Timers::Timer
  @timers.add(timer)
end
after(interval, &block) click to toggle source

Call the given block after the given interval

# File lib/timers.rb, line 16
def after(interval, &block)
  Timer.new(self, interval, false, &block)
end
after_milliseconds(interval, &block) click to toggle source

Call the given block after the given interval has expired. interval is measured in milliseconds.

Timer.new.after_milliseconds(25) { puts "fired!" }
# File lib/timers.rb, line 25
def after_milliseconds(interval, &block)
  after(interval / 1000.0, &block)
end
Also aliased as: after_ms
after_ms(interval, &block) click to toggle source
Alias for: after_milliseconds
every(interval, &block) click to toggle source

Call the given block periodically at the given interval

# File lib/timers.rb, line 31
def every(interval, &block)
  Timer.new(self, interval, true, &block)
end
fire(now = Time.now) click to toggle source

Fire all timers that are ready

# File lib/timers.rb, line 51
def fire(now = Time.now)
  time = now + 0.001 # Fudge 1ms in case of clock imprecision
  while (timer = @timers.first) && (time >= timer.time)
    @timers.delete timer
    timer.fire(now)
  end
end
wait() click to toggle source

Wait for the next timer and fire it

# File lib/timers.rb, line 36
def wait
  i = wait_interval
  sleep i if i
  fire
end
wait_interval(now = Time.now) click to toggle source

Interval to wait until when the next timer will fire

# File lib/timers.rb, line 43
def wait_interval(now = Time.now)
  timer = @timers.first
  return unless timer
  interval = timer.time - now
  interval > 0 ? interval : 0
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.