class NullIO
An IO class stub, this class is intended to be used as a placeholder for a class that uses an IO object. Example of such classes would be a logger that logs to an IO.
Constants
- ERR
STDERR shim
- IN
STDIN shim
- OUT
STDOUT shim
Public Class Methods
@param [Object] fd file descriptor @param [String] mode @param [Hash] options
# File lib/moon-null_io/null_io.rb, line 8 def initialize(fd, mode = 'w+', options = {}) @fd = fd @mode = mode @options = options @writable = !!(@mode =~ /[w\+a]/) @readable = !!(@mode =~ /[r\+]/) @binmode = !!(@mode =~ /[b]/) @closed = false end
Public Instance Methods
Pushes objects into the stream.
@param [Object] args @return [self]
# File lib/moon-null_io/null_io.rb, line 97 def <<(*args) print(*args) self end
Sets the IO into binary mode
@return [NullIO]
# File lib/moon-null_io/null_io.rb, line 59 def binmode NullIO.new(@fd, @mode + 'b', @options) end
Is the IO in binary mode?
@return [Boolean]
# File lib/moon-null_io/null_io.rb, line 66 def binmode? @binmode end
Closes the IO
@return [void]
# File lib/moon-null_io/null_io.rb, line 51 def close check_open @closed = true end
Is the IO closed?
@return [Boolean]
# File lib/moon-null_io/null_io.rb, line 21 def closed? @closed end
Flushes the stream
@return [self]
# File lib/moon-null_io/null_io.rb, line 114 def flush(*args) check_open self end
Writes args to the stream.
@param [Object] args @return [nil]
# File lib/moon-null_io/null_io.rb, line 83 def print(*args) write(*args) nil end
(see print
)
# File lib/moon-null_io/null_io.rb, line 89 def puts(*args) print(*args) end
Reads values from the stream, in the case of NullIO
, an empty string is returned.
@return [String] an empty string
# File lib/moon-null_io/null_io.rb, line 106 def read check_readable '' end
Writes a string to the stream, in the case of NullIO
.
@param [Object] args @return [Integer] number of bytes written
# File lib/moon-null_io/null_io.rb, line 74 def write(*args) check_writable args.inject(0) { |acc, o| acc + o.to_s.size } end
Private Instance Methods
Checks if the IO is open for operations
@raise IOError
# File lib/moon-null_io/null_io.rb, line 28 def check_open raise IOError, 'closed stream' if closed? end
Checks if the IO is open for reading
@raise IOError
# File lib/moon-null_io/null_io.rb, line 43 def check_readable check_open raise IOError, 'not opened for reading' unless @readable end
Checks if the IO is open for writing
@raise IOError
# File lib/moon-null_io/null_io.rb, line 35 def check_writable check_open raise IOError, 'not opened for writing' unless @writable end