Bird
Raised Fist0
Rubyprogramming~10 mins

Ractor for true parallelism in Ruby - Interactive Code Practice

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

Complete the code to create a new Ractor that prints 'Hello from Ractor!'.

Ruby
r = Ractor.new { puts [1] }
r.take
Drag options to blanks, or click blank then click option'
AHello from Ractor!
B'Hello from Ractor!'
C"Hello from Ractor!"
Dputs 'Hello from Ractor!'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Putting the entire puts statement inside the block as a string.
2fill in blank
medium

Complete the code to send a message 'ping' to the Ractor and receive a reply.

Ruby
r = Ractor.new do
  msg = Ractor.receive
  Ractor.yield(msg + [1])
end
r.send('ping')
puts r.take
Drag options to blanks, or click blank then click option'
A'pong'
B:pong
Cpong
D"pong"
Attempts:
3 left
💡 Hint
Common Mistakes
Using an unquoted pong which causes an error.
Using a symbol instead of a string.
3fill in blank
hard

Fix the error in the code to correctly pass data to the Ractor.

Ruby
r = Ractor.new([1]) do |data|
  data * 2
end
puts r.take
Drag options to blanks, or click blank then click option'
A[5]
B5, 10
C"5"
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a bare value instead of an array.
Passing multiple arguments without wrapping.
4fill in blank
hard

Fill both blanks to create a Ractor that calculates squares of numbers and returns a hash.

Ruby
numbers = [1, 2, 3]
squares = Ractor.new(numbers) do |nums|
  nums.each_with_object([1]) do |n, h|
    h[n] = n [2] 2
  end
end
puts squares.take
Drag options to blanks, or click blank then click option'
A{}
B[]
C**
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using an array instead of a hash for each_with_object.
Using multiplication instead of exponentiation.
5fill in blank
hard

Fill all three blanks to create a Ractor that filters even numbers and sums them.

Ruby
nums = [1, 2, 3, 4, 5, 6]
r = Ractor.new(nums) do |list|
  evens = list.select { |num| num [1] 2 [2] 0 }
  evens.[3](:+)
end
puts r.take
Drag options to blanks, or click blank then click option'
A%
B==
Creduce
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == for filtering evens.
Using sum instead of reduce (sum is not available in all Ruby versions).

Practice

(1/5)
1. What is the main purpose of using Ractor in Ruby?
easy
A. To run code truly in parallel on multiple CPU cores
B. To create graphical user interfaces
C. To handle database connections
D. To write single-threaded scripts

Solution

  1. Step 1: Understand Ractor's role

    Ractor is designed to enable true parallel execution in Ruby, using multiple CPU cores.
  2. Step 2: Compare options

    Options A, B, and C do not relate to parallelism or CPU core usage.
  3. Final Answer:

    To run code truly in parallel on multiple CPU cores -> Option A
  4. Quick Check:

    Ractor = true parallelism [OK]
Hint: Ractor means real parallel tasks in Ruby [OK]
Common Mistakes:
  • Confusing Ractor with threads
  • Thinking Ractor is for UI or database
  • Assuming Ractor runs code sequentially
2. Which of the following is the correct way to create a new Ractor in Ruby?
easy
A. Ractor.new { puts 'Hello from Ractor' }
B. Thread.new { puts 'Hello from Ractor' }
C. Process.spawn { puts 'Hello from Ractor' }
D. Fiber.new { puts 'Hello from Ractor' }

Solution

  1. Step 1: Identify Ractor creation syntax

    Ractor is created using Ractor.new with a block of code to run in parallel.
  2. Step 2: Eliminate other options

    Thread, Process, and Fiber are different concurrency tools, not Ractor.
  3. Final Answer:

    Ractor.new { puts 'Hello from Ractor' } -> Option A
  4. Quick Check:

    Use Ractor.new to create Ractors [OK]
Hint: Ractor.new starts a parallel task [OK]
Common Mistakes:
  • Using Thread.new instead of Ractor.new
  • Confusing Process.spawn with Ractor
  • Using Fiber.new which is cooperative, not parallel
3. What will be the output of this Ruby code using Ractor?
r = Ractor.new { 5 * 5 }
result = r.take
puts result
medium
A. RactorError
B. 5 * 5
C. nil
D. 25

Solution

  1. Step 1: Understand Ractor block execution

    The block { 5 * 5 } runs inside the Ractor and returns 25.
  2. Step 2: Use take to get the result

    r.take receives the value returned by the Ractor block, which is 25.
  3. Final Answer:

    25 -> Option D
  4. Quick Check:

    Ractor returns 25, take receives it [OK]
Hint: Ractor block returns value, take fetches it [OK]
Common Mistakes:
  • Expecting string '5 * 5' instead of calculation
  • Confusing take with yield
  • Assuming nil or error without communication
4. Identify the error in this Ractor code snippet:
r = Ractor.new do
  Ractor.yield 10
  20
end
puts r.take
puts r.take
medium
A. Ractor.new requires arguments
B. Second r.take call will cause Ractor::Closed error
C. No error, code runs fine
D. Calling Ractor.yield inside Ractor block is invalid

Solution

  1. Step 1: Understand Ractor.yield and r.take

    Ractor.yield sends a value to the main thread, and r.take receives it. The first r.take gets 10.
  2. Step 2: Analyze second r.take call

    The block returns 20 after yield, which is the final value. The second r.take gets 20. After that, the Ractor is closed. A third r.take would cause error, but here only two calls are made, so no error.
  3. Step 3: Check the code carefully

    Both r.take calls are valid; no error occurs.
  4. Final Answer:

    No error, code runs fine -> Option C
  5. Quick Check:

    Ractor.yield and take work twice safely [OK]
Hint: Ractor.yield sends values, take receives them sequentially [OK]
Common Mistakes:
  • Thinking Ractor.yield is invalid inside Ractor
  • Expecting error on second take call
  • Assuming Ractor.new needs arguments
5. You want to sum numbers from 1 to 100 using two Ractors running in parallel, each summing half the range. Which code correctly implements this?
hard
A. r1 = Ractor.new { (1..100).sum } r2 = Ractor.new { (1..100).sum } puts r1.take + r2.take
B. r1 = Ractor.new { (1..50).sum } r2 = Ractor.new { (51..100).sum } puts r1.take + r2.take
C. r1 = Thread.new { (1..50).sum } r2 = Thread.new { (51..100).sum } puts r1.value + r2.value
D. r1 = Ractor.new { (1..50).reduce(:+) } r2 = Ractor.new { (51..100).reduce(:+) } puts r1.take * r2.take

Solution

  1. Step 1: Split the range correctly

    We want two Ractors, one summing 1 to 50, the other 51 to 100.
  2. Step 2: Check the operations and combining results

    r1 = Ractor.new { (1..50).sum } r2 = Ractor.new { (51..100).sum } puts r1.take + r2.take sums each half and adds results correctly. r1 = Ractor.new { (1..100).sum } r2 = Ractor.new { (1..100).sum } puts r1.take + r2.take sums full range twice (wrong). r1 = Thread.new { (1..50).sum } r2 = Thread.new { (51..100).sum } puts r1.value + r2.value uses Thread, not Ractor. r1 = Ractor.new { (1..50).reduce(:+) } r2 = Ractor.new { (51..100).reduce(:+) } puts r1.take * r2.take multiplies sums instead of adding.
  3. Final Answer:

    r1 = Ractor.new { (1..50).sum } r2 = Ractor.new { (51..100).sum } puts r1.take + r2.take -> Option B
  4. Quick Check:

    Split range, sum halves, add results [OK]
Hint: Split work, sum halves in Ractors, then add [OK]
Common Mistakes:
  • Using Thread instead of Ractor
  • Summing full range twice
  • Multiplying sums instead of adding
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard