0
0
Rubyprogramming~20 mins

Ractor for true parallelism in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ractor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ractor communication code?

Consider this Ruby code using Ractors. What will it print?

Ruby
r = Ractor.new do
  (0...5).map { |i| i * 2 }
end

result = r.take
puts result.inspect
A[0, 2, 4, 6, 8]
Bnil
C0
DRactorError
Attempts:
2 left
💡 Hint

Remember that Ractor.new runs the block in parallel and take receives the result.

🧠 Conceptual
intermediate
1:30remaining
Which statement about Ractors is true?

Choose the correct statement about Ruby Ractors.

ARactors share all instance variables between them by default.
BRactors can directly modify each other's local variables.
CRactors communicate only by message passing and do not share memory.
DRactors run on the same thread and do not provide parallelism.
Attempts:
2 left
💡 Hint

Think about how Ractors achieve true parallelism safely.

🔧 Debug
advanced
2:00remaining
What error does this Ractor code raise?

What error will this code produce when run?

Ruby
r1 = Ractor.new do
  @counter = 0
  @counter += 1
  @counter
end

puts r1.take
ANameError
BRactor::IsolationError
CNoMethodError
DNo error, outputs 1
Attempts:
2 left
💡 Hint

Instance variables inside a Ractor are local to it.

Predict Output
advanced
2:00remaining
What is the output of this Ractor message passing example?

What will this Ruby code print?

Ruby
r = Ractor.new do
  msg = Ractor.receive
  msg * 3
end

r.send(7)
puts r.take
A21
B7
CRactor::ClosedError
Dnil
Attempts:
2 left
💡 Hint

Look at how the Ractor receives and processes the message.

🚀 Application
expert
3:00remaining
How many items are in the resulting array after this Ractor code runs?

Given this Ruby code using multiple Ractors, how many items will the final array contain?

Ruby
ractors = (0...3).map do |i|
  Ractor.new(i) do |index|
    Array.new(index + 2) { index }
  end
end

results = ractors.map(&:take)
final = results.flatten
puts final.size
A6
B9
C3
D7
Attempts:
2 left
💡 Hint

Count how many elements each Ractor creates and sum them.