shareable_proc(self: nil) { ... } -> ProcRuby 4.0 から[permalink][rdoc][edit][T] () { (?) [self: nil] → T } → ^(?) [self: nil] → T[T, S] (self: S) { (?) [self: S] → T } → ^(?) [self: S] → T-
与えられたブロックから shareable な Proc を作成して返します。
通常の Proc は shareable ではないため、他の Ractor に渡せません。このメソッドで作成した Proc は shareable になるため、複数の Ractor から利用できます。
作成される Proc の
selfは、キーワード引数selfで指定したオブジェクトになります。ブロックが外側のローカル変数を参照している場合、その値も shareable でなければなりません。- [PARAM]
self: -
作成する Proc の
selfにするオブジェクトを指定します。shareable でなければなりません。デフォルトは nil です。 - [EXCEPTION]
Ractor::IsolationError: -
selfが shareable でない場合や、ブロックが shareable でないオブジェクトを参照している場合に発生します。
例: self を指定pr = Ractor.shareable_proc { 42 } p Ractor.shareable?(pr) # => true p pr.lambda? # => false p Ractor.shareable?(proc { 42 }) # => false (通常の Proc は shareable ではない) # 他の Ractor に渡して呼び出せる p Ractor.new(pr) {|p| p.call }.value # => 42p Ractor.shareable_proc { self }.call # => nil p Ractor.shareable_proc(self: 42) { self + 1 }.call # => 43 x = 1 p Ractor.shareable_proc { x }.call # => 1 Ractor.shareable_proc(self: Object.new) { self } # ~> Ractor::IsolationError[SEE_ALSO] Ractor.shareable_lambda, Ractor.make_shareable
- [PARAM]