Skip to content

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

class method

Ractor.yield

Ractor.yield(msg, move: false) → nil

Send a message to the current ractor’s outgoing port to be consumed by take.

Example
r = Ractor.new {Ractor.yield 'Hello from ractor'}
puts r.take
# Prints: "Hello from ractor"

The method is blocking, and will return only when somebody consumes the sent message.

Example
r = Ractor.new do
  Ractor.yield 'Hello from ractor'
  puts "Ractor: after yield"
end
wait
puts "Still not taken"
puts r.take

This will print:

Still not taken
Hello from ractor
Ractor: after yield

If the outgoing port was closed with close_outgoing, the method will raise:

Example
r = Ractor.new do
  close_outgoing
  Ractor.yield 'Hello from ractor'
end
wait
# `yield': The outgoing-port is already closed (Ractor::ClosedError)

The meaning of move argument is the same as for send.

See also .shareable? #<< .select #[] also in 3.4 · 3.3 · 3.2 · 3.1