Fiber
Fiber allows you to pause and resume the execution of code explicitly, can be used to implement generators and coroutines.
yield and resume
fib = Fiber.new do
puts "2. fib can be paused using yield"
Fiber.yield
puts "4. fib resumed again"
end
puts "1. fib are paused when created, resume to start"
fib.resume
puts "3. paused fib can be resumed"
fib.resume
puts "5. done"
yield and resume with values
fib = Fiber.new do
puts Fiber.yield('1. yielded value')
'3. done'
end
puts fib.resume
puts fib.resume('2. resumed value')
Non-Blocking Fibers
Non-blocking Fibers is added in Ruby 3.0.
The async gem is built on top of non-blocking fibers.