0
0
Rubyprogramming~10 mins

Fiber for cooperative concurrency in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"Hello from Fiber!"
BFiber.yield
Cresume
Dputs
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.
2fill in blank
medium

Complete 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'
Ayield
BFiber.resume
CFiber.yield
Dresume
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.
3fill in blank
hard

Fix 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'
Afiber.start
BFiber.resume
Cfiber.run
Dfiber.resume
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.
4fill in blank
hard

Fill 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'
A"yielded value"
B"hello"
C"world"
D42
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.
5fill in blank
hard

Fill 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'
Ai
Bmessage
C"Hello"
D"Start"
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.