A Fiber is a lightweight concurrency tool in Ruby that allows you to pause and resume code execution manually, enabling cooperative multitasking.
Click to reveal answer
intermediate
How does Fiber differ from Thread in Ruby?
Fibers are cooperative, meaning they yield control explicitly, while Threads are preemptive and managed by the Ruby scheduler. Fibers are lighter and do not run in parallel.
Click to reveal answer
beginner
What method is used to start or resume a Fiber?
The Fiber#resume method is used to start or continue execution of a Fiber from where it last yielded.
Click to reveal answer
beginner
What does Fiber.yield do?
Fiber.yield pauses the current Fiber and returns control to the caller, allowing cooperative multitasking.
Click to reveal answer
intermediate
Why is Fiber considered cooperative concurrency?
Because Fibers only switch control when they explicitly yield, unlike threads which can be interrupted at any time by the scheduler.
Click to reveal answer
Which method starts or resumes a Fiber in Ruby?
AFiber#begin
BFiber#start
CFiber#run
DFiber#resume
✗ Incorrect
Fiber#resume is the correct method to start or continue a Fiber.
What happens when Fiber.yield is called?
AThe Fiber blocks other Fibers
BThe Fiber pauses and returns control to the caller
CThe Fiber runs in a new thread
DThe Fiber terminates immediately
✗ Incorrect
Fiber.yield pauses the Fiber and returns control to the caller, enabling cooperative multitasking.
Fibers in Ruby are best described as:
ACooperative concurrency units
BPreemptive threads
CParallel processes
DSynchronous functions
✗ Incorrect
Fibers require explicit yielding, so they are cooperative concurrency units.
Which of the following is NOT true about Fibers?
AThey run in parallel on multiple CPU cores
BThey allow manual control of execution flow
CThey are lighter than threads
DThey use <code>Fiber.yield</code> to pause execution
✗ Incorrect
Fibers do not run in parallel; they run cooperatively on a single thread.
What is the main advantage of using Fibers?
ASimplifies multi-thread synchronization
BAutomatic parallel execution
CEfficient cooperative multitasking with low overhead
DRuns code asynchronously without yielding
✗ Incorrect
Fibers provide efficient cooperative multitasking with less overhead than threads.
Explain how Fibers enable cooperative concurrency in Ruby.
Think about how Fibers pause themselves and let others run.
You got /4 concepts.
Describe the difference between Fibers and Threads in Ruby.
Consider how control is passed in Fibers vs Threads.
You got /4 concepts.
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