End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Ractor.recv
recv
ractor.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.0.5/ractor.rb, line 415
def self.receive
__builtin_cexpr! %q{
ractor_receive(ec, rb_ec_ractor_ptr(ec))
}
end
Alias of .receive
Receive an incoming message from the current Ractor’s incoming port’s queue, which was sent there by send.
Example
r = Ractor.new do v1 = Ractor.receive puts "Received: #{v1}" end r.send('message1') r.take # Here will be printed: "Received: message1"
Alternatively, private instance method receive may be used:
Example
r = Ractor.new do v1 = receive puts "Received: #{v1}" end r.send('message1') r.take # Here will be printed: "Received: message1"
The method blocks if the queue is empty.
Example
r = Ractor.new do puts "Before first receive" v1 = Ractor.receive puts "Received: #{v1}" v2 = Ractor.receive puts "Received: #{v2}" end wait puts "Still not received" r.send('message1') wait puts "Still received only one" r.send('message2') r.take
Output:
Before first receive Still not received Received: message1 Still received only one Received: message2
If close_incoming was called on the ractor, the method raises Ractor::ClosedError if there are no more messages in incoming queue:
Example
Ractor.new do close_incoming receive end wait # in `receive': The incoming port is already closed => #<Ractor:#2 test.rb:1 running> (Ractor::ClosedError)