Ruby学习笔记:Fiber

Fiber可以让程序分段执行。

fiber.rb

fiber = Fiber.new do
   puts "before yield"
   Fiber.yield 1
   puts "after yield"
   2
end

puts "before resume1"
puts fiber.resume
puts "before resume2"
puts fiber.resume
puts "before resume3"
puts fiber.resume
puts "after resume"

结果:

before resume1
before yield
1
before resume2
after yield
2
before resume3
fiber.rb:13:in `resume': dead fiber called (FiberError)
    from fiber.rb:13:in `<main>'

 

posted @ 2013-07-25 13:54  Leo Forest  阅读(378)  评论(0编辑  收藏  举报