0
0
Rubyprogramming~10 mins

Fiber for cooperative concurrency in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fiber for cooperative concurrency
Create Fiber
Start Fiber: runs until yield
Yield: pause Fiber, return control
Main resumes, can do other work
Resume Fiber: continues from yield
Fiber finishes or yields again
Repeat resume/yield until done
Fiber ends, control back to main
A Fiber is created and started, runs until it yields control back to main, which can then resume the Fiber later. This allows cooperative multitasking.
Execution Sample
Ruby
fiber = Fiber.new do
  puts "Step 1"
  Fiber.yield
  puts "Step 2"
end

fiber.resume
fiber.resume
This code creates a Fiber that prints 'Step 1', yields control, then prints 'Step 2' when resumed again.
Execution Table
StepActionFiber StateOutputControl Flow
1Create FiberCreated (not started)Main holds control
2fiber.resumeRunningStep 1Fiber runs until yield, then pauses
3Fiber.yieldPausedFiber yields control back to main
4Main continuesPausedMain can do other work
5fiber.resumeRunningStep 2Fiber resumes after yield
6Fiber block endsTerminatedFiber finishes, control back to main
💡 Fiber ends after second resume, no more code to run
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5Final
fibernilFiber object createdFiber paused after yieldFiber runningFiber terminated
Key Moments - 3 Insights
Why does the Fiber stop after the first resume even though the block has more code?
Because the Fiber calls Fiber.yield which pauses execution and returns control to main (see execution_table step 3). The Fiber resumes only when fiber.resume is called again.
What happens if we call fiber.resume after the Fiber has finished?
Calling resume on a terminated Fiber raises a FiberError because the Fiber has no more code to run (see execution_table step 6).
Does the main program run while the Fiber is paused?
Yes, after Fiber.yield pauses the Fiber (step 3), main regains control and can do other work until fiber.resume is called again (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Fiber state after the first fiber.resume call?
ARunning
BTerminated
CPaused
DCreated
💡 Hint
Check execution_table row 2 and 3 where Fiber runs then yields and pauses
At which step does the Fiber yield control back to main?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at execution_table step 3 where Fiber.yield is called
If we remove Fiber.yield from the code, what happens to the output?
A'Step 1' and 'Step 2' print immediately on first resume
BOnly 'Step 1' prints
CNo output prints
DFiber never terminates
💡 Hint
Without yield, Fiber runs entire block on first resume (see execution flow)
Concept Snapshot
Fiber.new { ... } creates a Fiber.
fiber.resume starts or continues it.
Fiber.yield pauses Fiber, returns control to main.
Main and Fiber cooperate by yielding and resuming.
Fiber ends when block finishes.
Useful for cooperative concurrency.
Full Transcript
This example shows how Ruby Fibers work for cooperative concurrency. First, a Fiber is created with Fiber.new and a block of code. When fiber.resume is called, the Fiber starts running and prints 'Step 1'. Then it calls Fiber.yield, which pauses the Fiber and returns control to the main program. The Fiber is now paused. The main program can do other work or call fiber.resume again to continue the Fiber. On the second resume, the Fiber prints 'Step 2' and finishes. Trying to resume a finished Fiber causes an error. This cooperative switching allows the program to manage multiple tasks by explicitly yielding and resuming Fibers.