class method
Ractor.new
(*untyped args, ?name: ::string) { (?) -> untyped } -> ::Ractor
Ractor.new(*args, name: nil) {|*args| block } → ractor
ractor.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ractor.rb, line 229
def self.new(*args, name: nil, &block)
b = block # TODO: builtin bug
raise ArgumentError, "must be called with a block" unless block
if __builtin_cexpr!("RBOOL(ruby_single_main_ractor)")
Kernel.warn("Ractor API is experimental and may change in future versions of Ruby.",
uplevel: 0, category: :experimental)
end
loc = caller_locations(1, 1).first
loc = "#{loc.path}:#{loc.lineno}"
__builtin_ractor_create(loc, name, args, b)
end
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>