views:

58

answers:

1

I'm trying to get a sample to work with Ruby 1.8.6. The problem is that the sample was written for Ruby 1.8.5.

My assumption is that the problem lies in the way the Thread is called, or not called. This is the sample:

class Timer
  def initialize(resolution)
    @resolution = resolution
    @queue = []

    Thread.new do
      while true
        dispatch
        sleep(@resolution)
      end
    end
  end

  def at(time, &block)
    time = time.to_f if time.kind_of?(Time)
    @queue.push [time, block]
  end

  private
  def dispatch
    now = Time.now.to_f
    ready, @queue = @queue.partition{|time, proc|  time <= now }
    ready.each {|time, proc| proc.call(time) }
  end
end


timer = Timer.new(0.01)
timer.at(Time.now + 3) { puts "Hello" }

I can't get it to work with 1.8.6 so I hope someone can show me how to make it compatible with 1.8.6

Thanks

+3  A: 

Your problem may be that the execution of main thread ends earlier then 3 second timeout passes, try to put sleep(3.2) at the end of your code. If it's not the case, please describe what you want to achieve and what happens when you try to do that

roddik
You were right. How could I oversee that. Thanks.
daddz