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

new(fd, mode = 'w+', options = {}) click to toggle source

@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

<<(*args) click to toggle source

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
binmode() click to toggle source

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
binmode?() click to toggle source

Is the IO in binary mode?

@return [Boolean]

# File lib/moon-null_io/null_io.rb, line 66
def binmode?
  @binmode
end
close() click to toggle source

Closes the IO

@return [void]

# File lib/moon-null_io/null_io.rb, line 51
def close
  check_open
  @closed = true
end
closed?() click to toggle source

Is the IO closed?

@return [Boolean]

# File lib/moon-null_io/null_io.rb, line 21
def closed?
  @closed
end
flush(*args) click to toggle source

Flushes the stream

@return [self]

# File lib/moon-null_io/null_io.rb, line 114
def flush(*args)
  check_open
  self
end
print(*args) click to toggle source

Writes args to the stream.

@param [Object] args @return [nil]

puts(*args) click to toggle source

(see print)

# File lib/moon-null_io/null_io.rb, line 89
def puts(*args)
  print(*args)
end
read() click to toggle source

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
write(*args) click to toggle source

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

check_open() click to toggle source

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
check_readable() click to toggle source

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
check_writable() click to toggle source

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