Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Fiber that prints a message.
Ruby
fiber = Fiber.new do
puts [1]
end
fiber.resume Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Fiber.yield instead of a string to print.
Forgetting to put the message inside quotes.
Trying to call resume inside the Fiber block.
✗ Incorrect
The Fiber block should print the string "Hello from Fiber!" using puts.
2fill in blank
mediumComplete the code to yield control back from the Fiber.
Ruby
fiber = Fiber.new do puts "Step 1" [1] puts "Step 2" end fiber.resume fiber.resume
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using yield instead of Fiber.yield inside the Fiber block.
Calling Fiber.resume inside the Fiber block.
Using resume without Fiber prefix.
✗ Incorrect
Fiber.yield pauses the Fiber and returns control to the caller.
3fill in blank
hardFix the error in the code to resume the Fiber correctly.
Ruby
fiber = Fiber.new do puts "Inside Fiber" end [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fiber.start which does not exist.
Calling Fiber.resume as a class method.
Using fiber.run which is not a Fiber method.
✗ Incorrect
To start or resume a Fiber, call fiber.resume.
4fill in blank
hardFill both blanks to create a Fiber that yields a value and resumes with a parameter.
Ruby
fiber = Fiber.new do value = Fiber.yield [1] puts "Received: #{value}" end fiber.resume fiber.resume([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a number instead of a string to resume.
Not yielding any value from the Fiber.
Using Fiber.resume instead of fiber.resume.
✗ Incorrect
The Fiber yields the string "yielded value" and resumes receiving "hello".
5fill in blank
hardFill all three blanks to create a Fiber that counts and yields each number, then resumes with a message.
Ruby
fiber = Fiber.new do (1..3).each do |i| message = Fiber.yield [1] puts "Count: #{i}, Message: #[2]" end end fiber.resume fiber.resume([3]) fiber.resume("Hi") fiber.resume("Bye")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of the variable i to yield.
Printing i instead of the received message.
Passing wrong values to resume.
✗ Incorrect
The Fiber yields the current count i, prints the received message, and resumes with "Start" first.