End of Life Ruby 3.0 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
ractor.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.0.5/ractor.rb, line 262
def self.new(*args, name: nil, &block)
b = block # TODO: builtin bug
raise ArgumentError, "must be called with a block" unless block
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.
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>