[Note: parts of this message were removed to make it a legal post.]
Hi, All.
Can anybody tell me how to implement kind of a timer in Ruby, similar to
the following one from Java?
timer = new Timer( 200, handler ); // call handler each 200 milliseconds
Here’s a simple one
#!/bin/env ruby
require ‘monitor’
class Timer
def initialize(interval, &handler)
raise ArgumentError, “Illegal interval” if interval < 0
extend MonitorMixin @run = true @th = Thread.new do
t = Time.now
while run?
t += interval
(sleep(t - Time.now) rescue nil) and
handler.call rescue nil
end
end
end
def stop
synchronize do @run = false
end @th.join
end