Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using Fiber in Ruby?
easy
A. To pause and resume tasks manually for cooperative multitasking
B. To create multiple threads that run in parallel automatically
C. To speed up code by running it on multiple CPU cores
D. To handle database connections asynchronously

Solution

  1. Step 1: Understand Fiber's role

    Fibers allow manual pausing and resuming of code execution, enabling cooperative multitasking.
  2. Step 2: Differentiate from threads

    Unlike threads, Fibers do not run in parallel automatically; they require explicit control.
  3. Final Answer:

    To pause and resume tasks manually for cooperative multitasking -> Option A
  4. Quick Check:

    Fiber = manual pause/resume [OK]
Hint: Fibers pause and resume tasks manually, not parallel threads [OK]
Common Mistakes:
  • Confusing Fibers with threads or parallelism
  • Thinking Fibers run automatically in background
  • Assuming Fibers speed up CPU usage
2. Which of the following is the correct way to create a Fiber in Ruby?
easy
A. fiber = Fiber.start { puts 'Hello' }
B. fiber = Fiber.new { puts 'Hello' }
C. fiber = Fiber.create { puts 'Hello' }
D. fiber = Fiber.run { puts 'Hello' }

Solution

  1. Step 1: Recall Fiber creation syntax

    Ruby uses Fiber.new with a block to create a new Fiber.
  2. Step 2: Check other options

    Methods like create, start, or run do not exist for Fiber creation.
  3. Final Answer:

    fiber = Fiber.new { puts 'Hello' } -> Option B
  4. Quick Check:

    Fiber creation = Fiber.new [OK]
Hint: Use Fiber.new with a block to create a Fiber [OK]
Common Mistakes:
  • Using non-existent Fiber methods like create or start
  • Forgetting to pass a block to Fiber.new
  • Confusing Fiber with Thread syntax
3. What will be the output of the following Ruby code?
fiber = Fiber.new do
  puts 'Step 1'
  Fiber.yield
  puts 'Step 2'
end

fiber.resume
puts 'Between'
fiber.resume
medium
A. Step 2 Between Step 1
B. Step 1 Step 2 Between
C. Between Step 1 Step 2
D. Step 1 Between Step 2

Solution

  1. Step 1: Trace first resume call

    Calling fiber.resume runs until Fiber.yield, printing 'Step 1'.
  2. Step 2: Trace code after first resume

    After yield, control returns to main, printing 'Between'.
  3. Step 3: Trace second resume call

    Second fiber.resume resumes after yield, printing 'Step 2'.
  4. Final Answer:

    Step 1 Between Step 2 -> Option D
  5. Quick Check:

    Fiber.yield pauses, resume continues [OK]
Hint: Fiber.yield pauses; resume continues after yield [OK]
Common Mistakes:
  • Assuming all code runs at first resume
  • Confusing order of prints around yield
  • Thinking yield stops Fiber permanently
4. Identify the error in this Fiber usage:
fiber = Fiber.new do
  puts 'Start'
  Fiber.yield
  puts 'End'
end

fiber.yield
fiber.resume
medium
A. Calling Fiber.yield outside a Fiber block causes an error
B. Fiber.resume must be called before Fiber.yield
C. Fiber.new requires a parameter for yield count
D. Fiber cannot print inside its block

Solution

  1. Step 1: Check where Fiber.yield is called

    The code calls fiber.yield outside the Fiber block, which is invalid.
  2. Step 2: Understand Fiber.yield usage

    Fiber.yield is a class method to pause current Fiber, not an instance method on Fiber objects.
  3. Final Answer:

    Calling Fiber.yield outside a Fiber block causes an error -> Option A
  4. Quick Check:

    Fiber.yield only inside Fiber block [OK]
Hint: Fiber.yield is called inside Fiber block only [OK]
Common Mistakes:
  • Calling fiber.yield instead of Fiber.yield
  • Expecting Fiber.resume before yield call
  • Thinking Fiber.new needs parameters for yield
5. Given two Fibers that alternate printing numbers, what is the output of this code?
fiber1 = Fiber.new do
  3.times do |i|
    puts "Fiber1: #{i}"
    Fiber.yield
  end
end

fiber2 = Fiber.new do
  3.times do |i|
    puts "Fiber2: #{i}"
    Fiber.yield
  end
end

while fiber1.alive? || fiber2.alive?
  fiber1.resume if fiber1.alive?
  fiber2.resume if fiber2.alive?
end
hard
A. Fiber2: 0 Fiber1: 0 Fiber2: 1 Fiber1: 1 Fiber2: 2 Fiber1: 2
B. Fiber1: 0 Fiber1: 1 Fiber1: 2 Fiber2: 0 Fiber2: 1 Fiber2: 2
C. Fiber1: 0 Fiber2: 0 Fiber1: 1 Fiber2: 1 Fiber1: 2 Fiber2: 2
D. Fiber1: 0 Fiber2: 0 Fiber2: 1 Fiber1: 1 Fiber1: 2 Fiber2: 2

Solution

  1. Step 1: Understand Fiber alternation

    Each Fiber prints a number then yields, pausing itself.
  2. Step 2: Trace the while loop

    The loop resumes fiber1 then fiber2 alternately while they are alive.
  3. Step 3: Follow output order

    Output alternates: Fiber1:0, Fiber2:0, Fiber1:1, Fiber2:1, Fiber1:2, Fiber2:2.
  4. Final Answer:

    Fiber1: 0 Fiber2: 0 Fiber1: 1 Fiber2: 1 Fiber1: 2 Fiber2: 2 -> Option C
  5. Quick Check:

    Fibers alternate resume and yield [OK]
Hint: Fibers alternate resume and yield to interleave output [OK]
Common Mistakes:
  • Assuming one Fiber runs completely before the other
  • Mixing order of Fiber resumes
  • Forgetting Fiber.yield pauses execution
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard