Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method · added in Ruby 4.0

Ractor.shareable_proc

[T] () { (?) [self: nil] -> T } -> ^(?) [self: nil] -> T
[T, S] (self: S) { (?) [self: S] -> T } -> ^(?) [self: S] -> T
Ractor.shareable_proc(self: nil){} → shareable proc

Returns a shareable copy of the given block’s Proc. The value of self in the Proc will be replaced with the value passed via the self: keyword, or nil if not given.

In a shareable Proc, access to any outer variables is prohibited.

Example
a = 42
Ractor.shareable_proc{ p a }
#=> can not isolate a Proc because it accesses outer variables (a). (Ractor::IsolationError)

The value of self in the Proc must be a shareable object.

Example
Ractor.shareable_proc(self: self){}
#=> self should be shareable: main (Ractor::IsolationError)

Ruby version changes

  1. 4.0