I'm trying to understand the behavior of the move
flag in Ruby's Ractor.select
method.
method signature: https://ruby-doc.org/3.2.1/Ractor.html#method-c-current
The description of the function's move flag in the documentation describes the flag in the following way:
move boolean flag defines whether yielded value should be copied (default) or moved.
This statement makes sense in a language with the ownership model in place like rust. But I'm trying to understand what it means for ruby
I ran a piece of code to experiment with the effect of this flag.
should_move = true
puts "MOVE: #{should_move}"
r1 = Ractor.new do
data = [{name: "tom"}, {name: "dick"}, {name: "harry"}]
puts "[INSIDE RACTOR] data value is #{data} and data object id is :#{data[0].object_id}"
Ractor.yield(data)
sleep 2
puts "[INSIDE RACTOR] data value is #{data} and data object id is :#{data[0].object_id}"
end
ractor, value = Ractor.select(r1, move: should_move)
puts "[OUTSIDE RACTOR] value is: #{value} and value[0] object_id is: #{value[0].object_id}"
p ractor
sleep 3
in the above code, the output was the same for both should_move = true && should_move = false.
MOVE: true
main.rb:4: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
[INSIDE RACTOR (before move)] data value is [{:name=>"tom"}, {:name=>"dick"}, {:name=>"harry"}] and data object id is :60
[OUTSIDE RACTOR (after move)] value is: [{:name=>"tom"}, {:name=>"dick"}, {:name=>"harry"}] and value[0] object_id is: 80
#<Ractor:#2 main.rb:4 blocking>
[INSIDE RACTOR (after move)] data value is [{:name=>"tom"}, {:name=>"dick"}, {:name=>"harry"}] and data object id is :60
and then I tried running the same thing, but this time, I set data
variable inside the ractor to an integer value as follows:
should_move = true
puts "MOVE: #{should_move}"
r1 = Ractor.new do
data = 12
puts "[INSIDE RACTOR] data value is #{data} and data object id is :#{data.object_id}"
Ractor.yield(data)
sleep 2
puts "[INSIDE RACTOR] data value is #{data} and data object id is :#{data.object_id}"
end
ractor, value = Ractor.select(r1, move: should_move)
puts "[OUTSIDE RACTOR] value is: #{value} and value object_id is: #{value.object_id}"
p ractor
sleep 3
this time, the output in this case was as follows:
MOVE: false
main.rb:4: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
[INSIDE RACTOR (before move)] data value is 12 and data object id is :25
[OUTSIDE RACTOR (after move)] value is: 12 and value[0] object_id is: 25
#<Ractor:#2 main.rb:4 blocking>
[INSIDE RACTOR (after move)] data value is 12 and data object id is :25
the move flag state did not affect the output in this case as well.