D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Ractor
/
Filename :
send-i.ri
back
Copy
U:RDoc::AnyMethod[iI" send:ETI"Ractor#send;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"RSend a message to a Ractor's incoming queue to be accepted by Ractor.receive.;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I"r = Ractor.new do ;TI" value = Ractor.receive ;TI" puts "Received #{value}" ;TI" end ;TI"r.send 'message' ;TI"## Prints: "Received: message" ;T:@format0o; ; [I"YThe method is non-blocking (will return immediately even if the ractor is not ready ;TI"to receive anything):;T@o;; [ I"r = Ractor.new {sleep(5)} ;TI"r.send('test') ;TI"puts "Sent successfully" ;TI"/# Prints: "Sent successfully" immediately ;T; 0o; ; [I"hAn attempt to send to a ractor which already finished its execution will raise Ractor::ClosedError.;T@o;; [I"r = Ractor.new {} ;TI"r.take ;TI" p r ;TI"*# "#<Ractor:#6 (irb):23 terminated>" ;TI"r.send('test') ;TI"A# Ractor::ClosedError (The incoming-port is already closed) ;T; 0o; ; [I"\If close_incoming was called on the ractor, the method also raises Ractor::ClosedError.;T@o;; [ I"r = Ractor.new do ;TI" sleep(500) ;TI" receive ;TI" end ;TI"r.close_incoming ;TI"r.send('test') ;TI"A# Ractor::ClosedError (The incoming-port is already closed) ;TI"M# The error is raised immediately, not when the ractor tries to receive ;T; 0o; ; [I"jIf the +obj+ is unshareable, by default it will be copied into the receiving ractor by deep cloning. ;TI"cIf <tt>move: true</tt> is passed, the object is _moved_ into the receiving ractor and becomes ;TI" inaccessible to the sender.;T@o;; [ I"2r = Ractor.new {puts "Received: #{receive}"} ;TI"msg = 'message' ;TI"r.send(msg, move: true) ;TI"r.take ;TI"p msg ;T; 0o; ; [I"This prints:;T@o;; [I"Received: message ;TI"Vin `p': undefined method `inspect' for #<Ractor::MovedObject:0x000055c99b9b69b8> ;T; 0o; ; [I"RAll references to the object and its parts will become invalid to the sender.;T@o;; [I"2r = Ractor.new {puts "Received: #{receive}"} ;TI"s = 'message' ;TI"ary = [s] ;TI"copy = ary.dup ;TI"r.send(ary, move: true) ;TI" ;TI"s.inspect ;TI"G# Ractor::MovedError (can not send any methods to a moved object) ;TI"ary.class ;TI"G# Ractor::MovedError (can not send any methods to a moved object) ;TI"copy.class ;TI"(# => Array, it is different object ;TI"copy[0].inspect ;TI"G# Ractor::MovedError (can not send any methods to a moved object) ;TI"E# ...but its item was still a reference to `s`, which was moved ;T; 0o; ; [I"IIf the object is shareable, <tt>move: true</tt> has no effect on it:;T@o;; [ I"2r = Ractor.new {puts "Received: #{receive}"} ;TI"s = 'message'.freeze ;TI"r.send(s, move: true) ;TI"-s.inspect #=> "message", still available;T; 0: @fileI"ractor.rb;T:0@omit_headings_from_table_of_contents_below0I"+ractor.send(msg, move: false) -> self ;T0[[I"<<;To;; [ ;@i;0I"(obj, move: false);T@iFI"Ractor;TcRDoc::NormalClass00