Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Ractor.new
Ractor.new(*args, name: nil) {|*args| block } → ractor
ractor.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/ractor.rb, line 273
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)")
warn("Ractor is experimental, and the behavior may change in future versions of Ruby! " \
"Also there are many implementation issues.", uplevel: 0, category: :experimental)
end
loc = caller_locations(1, 1).first
loc = "#{loc.path}:#{loc.lineno}"
__builtin_ractor_create(loc, name, args, b)
end
Create a new Ractor with args and a block.
The given block (Proc) will be 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.take # 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.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') {}; r.take p r #=> #<Ractor:#3 my ractor test.rb:1 terminated>