Module: Farce::Ractor

Includes:
Internal::MarshalSupport::Reject
Defined in:
lib/farce/ractor.rb,
lib/farce/_yard/ractor.rb

Overview

Entry point for Ractor support.

This is always a module, but if native Ractor support is available:

  • It gets mixed into Ractor. This has no effect beyond another entry in the inheritance chain, so is_a?(Farce::Ractor) and Farce::Ractor === ractor work as expected.
  • It delegates, wraps, or backports known Ractor class methods.

If not, then this module provides a polyfill for Ractors. With the following caveats:

  • It uses thread groups to keep track of Thread-to-Ractor associations.
  • All objects are considered shareable.
  • It does not monkey-patch Thread. This means in particular:
    • Thread.list will include all threads from all Ractors. Use Farce::Ractor.threads instead.
    • Thread.main will return the main thread of the main Ractor, even if called from a different Ractor. Use Farce::Ractor.main_thread instead.

Defined Under Namespace

Classes: Port

Constant Summary collapse

Error =

Either Ractor::Error or a subclass of RuntimeError if native Ractors are not available. Will pick up an error class defined by a shim/polyfill if one is loaded, like ractor-shim.

defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)
ClosedError =

Either Ractor::ClosedError or a subclass of StopIteration if native Ractors are not available. Will pick up an error class defined by a shim/polyfill if one is loaded, like ractor-shim.

defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)
IsolationError =

Either Ractor::IsolationError or a subclass of Error if native Ractors are not available. Will pick up an error class defined by a shim/polyfill if one is loaded, like ractor-shim.

defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)
MovedError =

Either Ractor::MovedError or a subclass of Error if native Ractors are not available. Will pick up an error class defined by a shim/polyfill if one is loaded, like ractor-shim.

defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)
RemoteError =

Either Ractor::RemoteError or a subclass of Error if native Ractors are not available. Will pick up an error class defined by a shim/polyfill if one is loaded, like ractor-shim.

defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)
UnsafeError =

Either Ractor::UnsafeError or a subclass of Error if native Ractors are not available. Will pick up an error class defined by a shim/polyfill if one is loaded, like ractor-shim.

defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)
MovedObject =

Either Ractor::MovedObject or a subclass of BasicObject if native Ractors are not available. Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.

defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](key) ⇒ BasicObject?

Retrieves a value from the current Ractor's storage by key.

Parameters:

  • key (Symbol) —

    The key to retrieve.

Returns:

  • (BasicObject, nil) —

    The value associated with the key, or nil if not found.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.[]=(key, value) ⇒ BasicObject

Stores a value in the current Ractor's storage by key.

Parameters:

  • key (Symbol) —

    The key to store the value under.

  • value (BasicObject) —

    The value to store.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.builtin? ⇒ Boolean

Note:

This method isn't part of the official Ractor API, but is compatible with the ractor-shim gem.

Returns true if native Ractors are available, false otherwise.

Returns:

  • (Boolean) —

    true if native Ractors are available, false otherwise.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.count ⇒ Integer

Returns the number of Ractors.

Returns:

  • (Integer) —

    The number of Ractors.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.current ⇒ Ractor

Returns the current Ractor instance.

Returns:

  • (Ractor) —

    The current Ractor instance.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.main ⇒ Ractor

Returns the main Ractor instance.

Returns:

  • (Ractor) —

    The main Ractor instance.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.main? ⇒ Boolean

Checks if the current Ractor is the main Ractor.

Returns:

  • (Boolean) —

    true if the current Ractor is the main Ractor, false otherwise.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.main_thread ⇒ Thread

Note:

This method is not part of the official Ractor API, but is a replacement for a Ractor-unaware Thread.main.

Returns the main thread of the current Ractor.

Returns:

  • (Thread) —

    The main thread of the current Ractor.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.make_shareable(object, copy: false) ⇒ BasicObject

Note:

If native Ractors aren't available, this method simply returns the object, as all objects are shareable.

Makes an object shareable between Ractors.

Parameters:

  • object (BasicObject) —

    The object to make shareable.

  • copy (Boolean) (defaults to: false) —

    Whether to create a copy of the object.

Returns:

  • (BasicObject) —

    The shareable object.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.new(*args, name: nil) {|*args| ... } ⇒ Ractor

Creates a new Ractor instance.

Parameters:

  • args (Array) —

    The arguments to pass to the Ractor's block. Will be copied if not shareable.

  • name (String, nil) (defaults to: nil) —

    The name of the Ractor, or nil for no name.

Yields:

  • (*args) —

    The block to execute in the new Ractor.

Yield Parameters:

  • args (Array) —

    The arguments passed to the Ractor.

Yield Returns:

  • (BasicObject) —

    The value returned by the Ractor's block.

Returns:

  • (Ractor) —

    The new Ractor instance.

Yield Receiver:

  • (Ractor) —

    The new Ractor instance.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.receive(timeout: nil) ⇒ BasicObject? Also known as: recv

Receives a message on the current Ractor's default port.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil) —

    The timeout in seconds, or nil for no timeout.

Returns:

  • (BasicObject, nil) —

    The received message, or nil if the timeout has been reached.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.select(*ractors_or_ports, timeout: nil) ⇒ Array(Object, BasicObject)?

Blocks the current Thread until one of the given Ractors or Ports produces a value, or until the timeout has been reached.

Returns an array containing the Ractor or Port that produced the value, and the value itself. Or nil if the timeout has been reached.

Parameters:

  • ractors_or_ports (Array<Ractor, Farce::Abstract::Port>) —

    The Ractors or Ports to select from.

  • timeout (Numeric, nil) (defaults to: nil) —

    The timeout in seconds, or nil for no timeout.

Returns:

  • (Array(Object, BasicObject), nil) —

    An array containing the Ractor or Port that produced the value, and the value itself, or nil if the timeout has been reached.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.shareable?(object) ⇒ Boolean

Note:

If native Ractors aren't available, this method always returns true, as all objects are shareable.

Checks if an object is shareable between Ractors.

Parameters:

  • object (BasicObject) —

    The object to check.

Returns:

  • (Boolean) —

    true if the object is shareable, false otherwise.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.shareable_lambda(self: nil) { ... } ⇒ Proc

Binds a lambda to the given self and makes it shareable between Ractors.

Parameters:

  • self (BasicObject) (defaults to: nil) —

    The object to bind as self.

Yields:

  • The block to convert into a shareable lambda.

Returns:

  • (Proc) —

    The shareable lambda.

See Also:

Yield Receiver:

  • (BasicObject) —

    The object passed as self (or nil)



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.shareable_proc(self: nil) { ... } ⇒ Proc

Binds a proc to the given self and makes it shareable between Ractors.

Parameters:

  • self (BasicObject) (defaults to: nil) —

    The object to bind as self.

Yields:

  • The block to convert into a shareable proc.

Returns:

  • (Proc) —

    The shareable proc.

See Also:

Yield Receiver:

  • (BasicObject) —

    The object passed as self (or nil)



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.shim? ⇒ Boolean

Note:

This method isn't part of the official Ractor API, but is compatible with the ractor-shim gem.

Returns false if native Ractors are available, true otherwise.

Returns:

  • (Boolean) —

    false if native Ractors are available, true otherwise.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.store_if_absent(key) { ... } ⇒ BasicObject

Stores a value in the current Ractor's storage by key if it is not already present. This method is thread-safe.

Parameters:

  • key (Symbol) —

    The key to store the value under.

Yields:

  • Block that computes the value to store if the key is absent.

Yield Returns:

  • (BasicObject) —

    The value to store if the key is absent.

Returns:

  • (BasicObject) —

    The existing or newly stored value.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

.threads ⇒ Array<Thread>

Note:

This method isn't part of the official Ractor API, but is a replacement for a Ractor-unaware Thread.list.

Returns the list of threads in the current Ractor.

Returns:

  • (Array<Thread>) —

    The list of threads in the current Ractor.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

Instance Method Details

#default_port ⇒ Ractor::Port

Returns The default port for the Ractor.

Returns:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#join ⇒ self

Blocks the current Thread until the Ractor has terminated.

Returns:

  • (self)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#main? ⇒ Boolean

Returns true if the Ractor is the main Ractor.

Returns:

  • (Boolean) —

    true if the Ractor is the main Ractor



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#monitor(port) ⇒ Boolean

Monitors the given port for termination of the Ractor. If the Ractor is already terminated, the port will receive the Ractor's status immediately. The port will receive wither :exited or :aborted depending on whether the Ractor terminated normally or with an unhandled exception.

Parameters:

Returns:

  • (Boolean) —

    true if the Ractor is running and the port was successfully monitored, false if the Ractor is already terminated and the port received the Ractor's status.

See Also:

Supported Ruby Versions:

  • CRuby 4.0+, JRuby, TruffleRuby



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#name ⇒ String?

Returns The name of the Ractor, or nil if it has no name.

Returns:

  • (String, nil) —

    The name of the Ractor, or nil if it has no name.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#receive(timeout: nil) ⇒ BasicObject? Also known as: recv

Receives a message on the Ractor's default port.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil) —

    The timeout in seconds, or nil for no timeout.

Returns:

  • (BasicObject, nil) —

    The received message, or nil if the timeout has been reached.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#send(message, move: false) ⇒ self Also known as: <<

Sends a message to the Ractor's default port.

Parameters:

  • message (BasicObject) —

    The message to send.

  • move (Boolean) (defaults to: false) —

    Whether to move the message to the Ractor if it isn't sharable.

Returns:

  • (self)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#take ⇒ BasicObject

Deprecated.

This method only exists on CRuby 3.x (outdated Ractor API). Use #value if you don't have to support CRuby 3.x, or use Port.

Receives a message from the Ractor. This may be the ractor's return value.

Returns:

  • (BasicObject) —

    The received message.

Supported Ruby Versions:

  • CRuby 3.x



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#unmonitor(port) ⇒ self

Stops monitoring the given port for termination of the Ractor. If the port was not being monitored, this method has no effect.

Returns:

  • (self)

See Also:

Supported Ruby Versions:

  • CRuby 4.0+, JRuby, TruffleRuby



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end

#value ⇒ BasicObject

Blocks the current Thread until the Ractor has terminated, and returns the value returned by the Ractor's block. Will re-raise any unhandled exception raised in the Ractor's block.

Returns:

  • (BasicObject) —

    The value returned by the Ractor's block.

Supported Ruby Versions:

  • CRuby 4.0+, JRuby, TruffleRuby



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/farce/ractor.rb', line 217

module Ractor
  include Internal::MarshalSupport::Reject
  ::Ractor.include(self) if Internal.native_ractors?

  # @!parse
  #   # @!scope class
  #   alias recv receive
  #   alias recv receive
  #   alias << send

  # Either `Ractor::Error` or a subclass of `RuntimeError` if native Ractors are not available.
  # @!macro RactorError_note
  #   Will pick up an error class defined by a shim/polyfill if one is loaded,
  #   like [ractor-shim](https://github.com/eregon/ractor-shim).
  Error = defined?(::Ractor::Error) ? ::Ractor::Error : Class.new(RuntimeError)

  # Either `Ractor::ClosedError` or a subclass of `StopIteration` if native Ractors are not available.
  # @!macro RactorError_note
  ClosedError = defined?(::Ractor::ClosedError) ? ::Ractor::ClosedError : Class.new(StopIteration)

  # Either `Ractor::IsolationError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  IsolationError = defined?(::Ractor::IsolationError) ? ::Ractor::IsolationError : Class.new(Error)

  # Either `Ractor::MovedError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  MovedError = defined?(::Ractor::MovedError) ? ::Ractor::MovedError : Class.new(Error)

  # Either `Ractor::RemoteError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  RemoteError = defined?(::Ractor::RemoteError) ? ::Ractor::RemoteError : Class.new(Error)

  # Either `Ractor::UnsafeError` or a subclass of {Error} if native Ractors are not available.
  # @!macro RactorError_note
  UnsafeError = defined?(::Ractor::UnsafeError) ? ::Ractor::UnsafeError : Class.new(Error)

  # Either `Ractor::MovedObject` or a subclass of `BasicObject` if native Ractors are not available.
  # Cannot be instantiated directly, but can be used to check if an object has been moved to another Ractor.
  MovedObject = defined?(::Ractor::MovedObject) ? ::Ractor::MovedObject :
    Class.new(BasicObject) { def self.new(...) = raise TypeError, "allocator undefined for #{inspect}" }

  extend Internal::RactorMethods

  def self.const_missing(name) = name == :Port ? Internal::BasePort : super
  private_class_method :const_missing

  # @!visibility private
  def self.included(base)
    return if base.const_defined?(:Port, false)
    base.const_set(:Port, Internal::BasePort)
  end
end