Skip to content
class method

Ractor.new

(*untyped args, ?name: ::string) { (?) -> untyped } -> ::Ractor
Ractor.new(*args, name: nil) {|*args| block } → ractor

Creates a new Ractor with args and a block.

The given block (Proc) is isolated (can’t access any outer variables). self inside the block will refer to the current Ractor.

Example
r = Ractor.new { puts "Hi, I am #{self.inspect}" }
r.join
# Prints "Hi, I am #<Ractor:#2 test.rb:1 running>"

Any args passed are propagated to the block arguments by the same rules as objects sent via send/Ractor.receive. If an argument in args is not shareable, it will be copied (via deep cloning, which might be inefficient).

Example
arg = [1, 2, 3]
puts "Passing: #{arg} (##{arg.object_id})"
r = Ractor.new(arg) {|received_arg|
  puts "Received: #{received_arg} (##{received_arg.object_id})"
}
r.join
# Prints:
#   Passing: [1, 2, 3] (#280)
#   Received: [1, 2, 3] (#300)

Ractor’s name can be set for debugging purposes:

Example
r = Ractor.new(name: 'my ractor') {}; r.join
p r
#=> #<Ractor:#3 my ractor test.rb:1 terminated>
See also .make_shareable .receive .main? .recv also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0