Class: IO::LikeHelpers::DuplexedIO
- Inherits:
-
DelegatedIO
- Object
- AbstractIO
- DelegatedIO
- IO::LikeHelpers::DuplexedIO
- Defined in:
- lib/io/like_helpers/duplexed_io.rb
Overview
This class encapsulates 2 streams (one readable, one writable) into a single stream. It is primarily intended to serve as an ancestor for IO::Like and should not be used directly.
Direct Known Subclasses
Instance Method Summary collapse
-
#close ⇒ nil, ...
Closes this stream.
-
#close_on_exec=(close_on_exec) ⇒ Boolean
Sets the close-on-exec flag for the underlying file descriptors of the delegates.
-
#close_read ⇒ nil, ...
Closes the readable delegate.
-
#close_write ⇒ nil, ...
Closes the writable delegate.
-
#closed? ⇒ Boolean
Returns
trueif both delegates are closed andfalseotherwise. -
#closed_read? ⇒ Boolean
Returns
trueif the readable delegate is closed andfalseotherwise. -
#closed_write? ⇒ Boolean
Returns
trueif the writable delegate is closed andfalseotherwise. -
#initialize(delegate_r, delegate_w = delegate_r, autoclose: true) ⇒ DuplexedIO
constructor
Creates a new intance of this class.
-
#inspect ⇒ String
A string representation of this object.
-
#nonblock=(nonblock) ⇒ Boolean
Sets the blocking mode for the stream.
-
#pwrite(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate_w.write(*args, **kwargs, &b)` after asserting that the stream is writable.
-
#readable? ⇒ Boolean
Returns
trueif the stream is readable andfalseotherwise. -
#writable? ⇒ Boolean
Returns
trueif the stream is writable andfalseotherwise. -
#write(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate_w.write(*args, **kwargs, &b)` after asserting that the stream is writable.
Methods inherited from DelegatedIO
#advise, #autoclose=, #autoclose?, #close_on_exec?, create_finalizer, #fcntl, #fdatasync, #fileno, #fsync, #ioctl, #nonblock?, #nread, #path, #pid, #pread, #read, #ready?, #seek, #stat, #to_io, #tty?, #wait
Methods inherited from AbstractIO
#advise, #close_on_exec?, #fcntl, #fdatasync, #fileno, #fsync, #ioctl, #nonblock, #nonblock?, #nread, open, #path, #pid, #pread, #read, #ready?, #seek, #stat, #to_io, #tty?, #wait
Constructor Details
#initialize(delegate_r, delegate_w = delegate_r, autoclose: true) ⇒ DuplexedIO
Creates a new intance of this class.
19 20 21 22 23 24 25 26 27 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 19 def initialize(delegate_r, delegate_w = delegate_r, autoclose: true) raise ArgumentError, 'delegate_r cannot be nil' if delegate_r.nil? raise ArgumentError, 'delegate_w cannot be nil' if delegate_w.nil? @delegate_w = delegate_w @closed_write = false super(delegate_r, autoclose: autoclose) end |
Instance Method Details
#close ⇒ nil, ...
Closes this stream.
The delegates are closed if autoclose is enabled for the stream.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 37 def close return nil if closed? begin result = close_write ensure # Complete the closing process if the writable delegate closed normally or # an exception was raised. result = close_read unless Symbol === result end result end |
#close_on_exec=(close_on_exec) ⇒ Boolean
Sets the close-on-exec flag for the underlying file descriptors of the delegates.
Note that setting this to false can lead to file descriptor leaks in multithreaded applications that fork and exec or use the system method.
133 134 135 136 137 138 139 140 141 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 133 def close_on_exec=(close_on_exec) return super unless duplexed? assert_open delegate_w.close_on_exec = delegate_r.close_on_exec = close_on_exec close_on_exec end |
#close_read ⇒ nil, ...
Closes the readable delegate.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 79 def close_read return nil if closed_read? begin result = delegate_r.close if @autoclose ensure # Complete the closing process if the delegate closed normally or an # exception was raised. unless Symbol === result @closed_write = true unless duplexed? @closed = true @delegate = @delegate_w disable_finalizer enable_finalizer if @autoclose && ! closed? end end result end |
#close_write ⇒ nil, ...
Closes the writable delegate.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 105 def close_write return nil if closed_write? begin result = delegate_w.close if @autoclose ensure # Complete the closing process if the delegate closed normally or an # exception was raised. unless Symbol === result @closed = true unless duplexed? @closed_write = true @delegate_w = @delegate disable_finalizer enable_finalizer if @autoclose && ! closed? end end result end |
#closed? ⇒ Boolean
Returns true if both delegates are closed and false otherwise.
61 62 63 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 61 def closed? closed_read? && closed_write? end |
#closed_read? ⇒ Boolean
Returns true if the readable delegate is closed and false otherwise.
55 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 55 alias_method :closed_read?, :closed? |
#closed_write? ⇒ Boolean
Returns true if the writable delegate is closed and false otherwise.
69 70 71 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 69 def closed_write? @closed_write end |
#inspect ⇒ String
Returns a string representation of this object.
145 146 147 148 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 145 def inspect return super unless duplexed? "<#{self.class}:#{delegate_r.inspect}, #{delegate_w.inspect}>" end |
#nonblock=(nonblock) ⇒ Boolean
Sets the blocking mode for the stream.
154 155 156 157 158 159 160 161 162 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 154 def nonblock=(nonblock) return super unless duplexed? assert_open delegate_w.nonblock = delegate_r.nonblock = nonblock nonblock end |
#pwrite(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate_w.write(*args, **kwargs, &b)` after asserting that the stream is writable.
167 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 167 delegate :pwrite, to: :delegate_w, assert: :writable |
#readable? ⇒ Boolean
Returns true if the stream is readable and false otherwise.
173 174 175 176 177 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 173 def readable? return false if closed_read? return @readable if defined?(@readable) && ! @readable.nil? @readable = delegate_r.readable? end |
#writable? ⇒ Boolean
Returns true if the stream is writable and false otherwise.
188 189 190 191 192 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 188 def writable? return false if closed_write? return @writable if defined?(@writable) && ! @writable.nil? @writable = delegate_w.writable? end |
#write(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate_w.write(*args, **kwargs, &b)` after asserting that the stream is writable.
182 |
# File 'lib/io/like_helpers/duplexed_io.rb', line 182 delegate :write, to: :delegate_w, assert: :writable |