D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Ractor
/
Filename :
receive_if-c.ri
back
Copy
U:RDoc::AnyMethod[iI"receive_if:ETI"Ractor::receive_if;TT:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"%Receive only a specific message.;To:RDoc::Markup::BlankLine o; ; [I"QInstead of Ractor.receive, Ractor.receive_if can be given a pattern (or any ;TI"Xfilter) in a block and you can choose the messages to accept that are available in ;TI""your ractor's incoming queue.;T@o:RDoc::Markup::Verbatim; [I"r = Ractor.new do ;TI"? p Ractor.receive_if{|msg| msg.match?(/foo/)} #=> "foo3" ;TI"? p Ractor.receive_if{|msg| msg.match?(/bar/)} #=> "bar1" ;TI"? p Ractor.receive_if{|msg| msg.match?(/baz/)} #=> "baz2" ;TI" end ;TI"r << "bar1" ;TI"r << "baz2" ;TI"r << "foo3" ;TI"r.take ;T:@format0o; ; [I"This will output:;T@o;; [I" foo3 ;TI" bar1 ;TI" baz2 ;T; 0o; ; [ I"YIf the block returns a truthy value, the message is removed from the incoming queue ;TI"and returned. ;TI"\Otherwise, the message remains in the incoming queue and the next messages are checked ;TI"by the given block.;T@o; ; [I"JIf there are no messages left in the incoming queue, the method will ;TI"%block until new messages arrive.;T@o; ; [I"ZIf the block is escaped by break/return/exception/throw, the message is removed from ;TI"?the incoming queue as if a truthy value had been returned.;T@o;; [I"r = Ractor.new do ;TI"7 val = Ractor.receive_if{|msg| msg.is_a?(Array)} ;TI", puts "Received successfully: #{val}" ;TI" end ;TI" ;TI"r.send(1) ;TI"r.send('test') ;TI" wait ;TI"2puts "2 non-matching sent, nothing received" ;TI"r.send([1, 2, 3]) ;TI" wait ;T; 0o; ; [I"Prints:;T@o;; [I"+2 non-matching sent, nothing received ;TI"&Received successfully: [1, 2, 3] ;T; 0o; ; [I"SNote that you can not call receive/receive_if in the given block recursively. ;TI"LYou should not do any tasks in the block other than message filtration.;T@o;; [I"Ractor.current << true ;TI"-Ractor.receive_if{|msg| Ractor.receive} ;TI"O#=> `receive': can not call receive/receive_if recursively (Ractor::Error);T; 0: @fileI"ractor.rb;T:0@omit_headings_from_table_of_contents_below0I"-Ractor.receive_if {|msg| block } -> msg ;T0[ I" (&b);T@RFI"Ractor;TcRDoc::NormalClass00