📖 Scenario: You are building a simple program that uses Ruby Fiber to switch between two tasks cooperatively. This simulates how two workers take turns doing their jobs without interrupting each other.
🎯 Goal: Create two Fiber objects that print messages in turns, showing how cooperative concurrency works with Fiber.yield and Fiber.resume.
📋 What You'll Learn
Create two Fiber objects named fiber1 and fiber2.
Each fiber should print a message and then yield control.
Use a loop to resume each fiber alternately two times.
Print the messages exactly as specified.
💡 Why This Matters
🌍 Real World
Fibers let programs switch between tasks without waiting for one to finish, useful in games, network servers, or UI where tasks share time smoothly.
💼 Career
Understanding fibers helps in writing efficient Ruby programs that handle multiple tasks cooperatively, improving responsiveness and resource use.
Progress0 / 4 steps
1
Create two Fiber objects
Create two Fiber objects called fiber1 and fiber2. fiber1 should print "Fiber 1: Step 1" and then yield. fiber2 should print "Fiber 2: Step 1" and then yield.
Ruby
Hint
Use Fiber.new do ... end to create fibers. Inside each, print the message and call Fiber.yield to pause.
2
Add second steps inside fibers
Inside fiber1, after the first yield, print "Fiber 1: Step 2". Inside fiber2, after the first yield, print "Fiber 2: Step 2". Add these lines after the first Fiber.yield in each fiber.
Ruby
Hint
After Fiber.yield, add a puts line for the second step message inside each fiber.
3
Create a loop to resume fibers alternately
Create a 2.times do loop. Inside the loop, resume fiber1 and then resume fiber2 to switch between them.
Ruby
Hint
Use 2.times do to repeat two times. Inside, call fiber1.resume then fiber2.resume.
4
Run the program and print the output
Run the program as is. The output should show the fibers printing their steps alternately. No extra code needed, just run the existing code.
Ruby
Hint
Just run the program. You will see the fibers print their messages in turns twice.
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
Step 1: Understand Fiber's role
Fibers allow manual pausing and resuming of code execution, enabling cooperative multitasking.
Step 2: Differentiate from threads
Unlike threads, Fibers do not run in parallel automatically; they require explicit control.
Final Answer:
To pause and resume tasks manually for cooperative multitasking -> Option A
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
Step 1: Recall Fiber creation syntax
Ruby uses Fiber.new with a block to create a new Fiber.
Step 2: Check other options
Methods like create, start, or run do not exist for Fiber creation.
Final Answer:
fiber = Fiber.new { puts 'Hello' } -> Option B
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
Step 1: Trace first resume call
Calling fiber.resume runs until Fiber.yield, printing 'Step 1'.
Step 2: Trace code after first resume
After yield, control returns to main, printing 'Between'.
Step 3: Trace second resume call
Second fiber.resume resumes after yield, printing 'Step 2'.
Final Answer:
Step 1
Between
Step 2 -> Option D
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
Step 1: Check where Fiber.yield is called
The code calls fiber.yield outside the Fiber block, which is invalid.
Step 2: Understand Fiber.yield usage
Fiber.yield is a class method to pause current Fiber, not an instance method on Fiber objects.
Final Answer:
Calling Fiber.yield outside a Fiber block causes an error -> Option A
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