1

I'm trying to create a Celluloid Actor that can accept a &block as a param and execute it every n seconds. However when I call the block from within the every loop, it never seems to do anything?

Take this example claass:

require 'celluloid/autostart'

class FooBar
  include Celluloid

  def initialize(&block)
    @block = block

    execute_block

    every(2) do
      puts "tick"
      execute_block
    end
  end

  def execute_block
    puts "tock"
    @block.call
  end

end

If I try the following:

a = FooBar.new { puts "Hi" }

Then I get this confusing output:

tock
Hi
tick
tock
tick
tock
tick
tock
tick
tock
tick
...

You can see that when I call execute_block the first time from outside the every block it works fine, and you can see that the every Timer is working correctly... But the block doesn't seem to every get called?

Anyone able to shed some light on what I'm doing wrong?

UPDATE: I noticed that if I moved the puts "tock" beneath the @block.call it would never get output at all...?

tob1k
  • 11
  • 3

0 Answers0