Skip to content
instance method

Ractor::Port#<<

(T obj, ?move: ::boolish) -> self
<<(obj, move: false)

Alias of #send

Sends a message to the port to be accepted by port.receive.

Example
port = Ractor::Port.new
r = Ractor.new(port) do |port|
  port.send 'message'
end
value = port.receive
puts "Received #{value}"
# Prints: "Received: message"

The method is non-blocking (it will return immediately even if the ractor that created the port is not ready to receive anything):

Example
port = Ractor::Port.new
r = Ractor.new(port) do |port|
  port.send 'test'
  puts "Sent successfully"
  # Prints: "Sent successfully" immediately
end

An attempt to send to a closed port will raise Ractor::ClosedError.

Example
r = Ractor.new {Ractor::Port.new}
r.join
p r
# "#<Ractor:#6 (irb):23 terminated>"
port = r.value
port.send('test') # raise Ractor::ClosedError

If the obj is unshareable, by default it will be copied into the receiving ractor by deep cloning.

If the object is shareable, a reference to the object will be sent to the receiving ractor.

See also #send .new #close #closed? also in 4.1