Ruby 关于 Fiber.yield 参数的理解

towonzhou · September 27, 2013 · 2296 hits

Fiber.yield 后面的为 fiber.resume 的返回值。 而下一次 resume 传入的参数为 Fiber.yield 的返回值。

f = Fiber.new do |a|
  c = Fiber.yield a+100
  p c
end

p f.resume(10)  #=> 110 
f.resume(100)  #=> 100

第一次的参数是 10,返回的是 a+10,返回给 f.resume(10) 继续调用 f.resume,传入 100,此时 100 作为 Fiber.yield 的返回值赋给 c,所以 c 是 100

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.