Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Ractor.new

Ractor.new(*args, name: nil) {|*args| block } → ractor

Create a new Ractor with args and a block.

A block (Proc) will be isolated (can’t access to outer variables). self inside the block will refer to the current Ractor.

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

args passed to the method would be propagated to block args by the same rules as objects passed through send/Ractor.receive: if args are not shareable, they 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.take
# 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') {}
p r
#=> #<Ractor:#3 my ractor test.rb:1 terminated>
See also .make_shareable .receive .main .receive_if also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0