0
0
Rubyprogramming~10 mins

Ractor for true parallelism 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 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).