rubyfiber

How to create a fiber using 'method' instead of a code block


Instead of doing this,

@fiber = Fiber.new do
  # ...
end

I am trying to do something like this:

def test
  yield
  # ...
end

def create(process)
  @fiber = Fiber.new(process)
end

create(method(:test))

How can I make the Fiber code look like the completed part?


Solution

  • Just convert the method to block:

    def test
      Fiber.yield 2.71
      Fiber.yield 3.14
      Fiber.yield 42
    end
    
    def create(&process)
      @fiber = Fiber.new(&process)
    end
    
    create(&method(:test))
    
    @fiber.resume # => 2.71
    @fiber.resume # => 3.14
    @fiber.resume # => 42