@@ -350,68 +350,6 @@ class Ractor
350350 #
351351 def self.receive : () -> untyped
352352
353- # <!--
354- # rdoc-file=ractor.rb
355- # - Ractor.receive_if {|msg| block } -> msg
356- # -->
357- # Receive only a specific message.
358- #
359- # Instead of Ractor.receive, Ractor.receive_if can be given a pattern (or any
360- # filter) in a block and you can choose the messages to accept that are
361- # available in your ractor's incoming queue.
362- #
363- # r = Ractor.new do
364- # p Ractor.receive_if{|msg| msg.match?(/foo/)} #=> "foo3"
365- # p Ractor.receive_if{|msg| msg.match?(/bar/)} #=> "bar1"
366- # p Ractor.receive_if{|msg| msg.match?(/baz/)} #=> "baz2"
367- # end
368- # r << "bar1"
369- # r << "baz2"
370- # r << "foo3"
371- # r.take
372- #
373- # This will output:
374- #
375- # foo3
376- # bar1
377- # baz2
378- #
379- # If the block returns a truthy value, the message is removed from the incoming
380- # queue and returned. Otherwise, the message remains in the incoming queue and
381- # the next messages are checked by the given block.
382- #
383- # If there are no messages left in the incoming queue, the method will block
384- # until new messages arrive.
385- #
386- # If the block is escaped by break/return/exception/throw, the message is
387- # removed from the incoming queue as if a truthy value had been returned.
388- #
389- # r = Ractor.new do
390- # val = Ractor.receive_if{|msg| msg.is_a?(Array)}
391- # puts "Received successfully: #{val}"
392- # end
393- #
394- # r.send(1)
395- # r.send('test')
396- # wait
397- # puts "2 non-matching sent, nothing received"
398- # r.send([1, 2, 3])
399- # wait
400- #
401- # Prints:
402- #
403- # 2 non-matching sent, nothing received
404- # Received successfully: [1, 2, 3]
405- #
406- # Note that you can not call receive/receive_if in the given block recursively.
407- # You should not do any tasks in the block other than message filtration.
408- #
409- # Ractor.current << true
410- # Ractor.receive_if{|msg| Ractor.receive}
411- # #=> `receive': can not call receive/receive_if recursively (Ractor::Error)
412- #
413- def self.receive_if : () { (untyped ) -> boolish } -> untyped
414-
415353 # <!--
416354 # rdoc-file=ractor.rb
417355 # - recv()
0 commit comments