class method
Ractor.make_shareable
[T] (T obj, ?copy: ::boolish) -> T
Ractor.make_shareable(obj, copy: false) → shareable_obj
ractor.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ractor.rb, line 459
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
Makes 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, it will copy objects before freezing them, and will not modify obj or its internal objects.
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.