class Channel
Provides a basic multi-thread/multi-process uni-directional communication channel.
Definitions
def initialize
Initialize the channel using a pipe.
Implementation
def initialize
@in, @out = ::IO.pipe
end
attr :in
The input end of the pipe.
Signature
-
attribute
IO
attr :out
The output end of the pipe.
Signature
-
attribute
IO
def close_read
Close the input end of the pipe.
Implementation
def close_read
@in.close
end
def close_write
Close the output end of the pipe.
Implementation
def close_write
@out.close
end
def close
Close both ends of the pipe.
Implementation
def close
close_read
close_write
end
def receive
Receive an object from the pipe.
Internally, prefers to receive newline formatted JSON, otherwise returns a hash table with a single key :line
which contains the line of data that could not be parsed as JSON.
Signature
-
returns
Hash
Implementation
def receive
if data = @in.gets
begin
return JSON.parse(data, symbolize_names: true)
rescue
return {line: data}
end
end
end