End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Ractor.make_shareable
Ractor.make_shareable(obj, copy: false) → shareable_obj
ractor.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.1.3/ractor.rb, line 814
def self.make_shareable obj, copy: false
if copy
__builtin_cexpr! %q{
rb_ractor_make_shareable_copy(obj);
}
else
__builtin_cexpr! %q{
rb_ractor_make_shareable(obj);
}
end
end
Make obj shareable between ractors.
obj and all the objects it refers to will be frozen, unless they are already shareable.
If copy keyword is true, the method will copy objects before freezing them This is safer option but it can take be slower.
Note that the specification and implementation of this method are not mature and may be changed in the future.
Example
obj = ['test'] Ractor.shareable?(obj) #=> false Ractor.make_shareable(obj) #=> ["test"] Ractor.shareable?(obj) #=> true obj.frozen? #=> true obj[0].frozen? #=> true # Copy vs non-copy versions: obj1 = ['test'] obj1s = Ractor.make_shareable(obj1) obj1.frozen? #=> true obj1s.object_id == obj1.object_id #=> true obj2 = ['test'] obj2s = Ractor.make_shareable(obj2, copy: true) obj2.frozen? #=> false obj2s.frozen? #=> true obj2s.object_id == obj2.object_id #=> false obj2s[0].object_id == obj2[0].object_id #=> false
See also the “Shareable and unshareable objects” section in the Ractor class docs.