Class: NullIO
- Inherits:
-
Object
- Object
- NullIO
- Defined in:
- lib/moon-null_io/null_io.rb
Overview
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.
Constant Summary collapse
- IN =
STDIN shim
new 0, 'r'
- OUT =
STDOUT shim
new 1, 'w'
- ERR =
STDERR shim
new 2, 'w'
Instance Method Summary collapse
-
#<<(*args) ⇒ self
Pushes objects into the stream.
-
#binmode ⇒ NullIO
Sets the IO into binary mode.
-
#binmode? ⇒ Boolean
Is the IO in binary mode?.
-
#close ⇒ void
Closes the IO.
-
#closed? ⇒ Boolean
Is the IO closed?.
-
#flush(*args) ⇒ self
Flushes the stream.
-
#initialize(fd, mode = 'w+', options = {}) ⇒ NullIO
constructor
A new instance of NullIO.
-
#print(*args) ⇒ nil
Writes args to the stream.
-
#puts(*args) ⇒ nil
Writes args to the stream.
-
#read ⇒ String
Reads values from the stream, in the case of NullIO, an empty string is returned.
-
#write(*args) ⇒ Integer
Writes a string to the stream, in the case of NullIO.
Constructor Details
#initialize(fd, mode = 'w+', options = {}) ⇒ NullIO
Returns a new instance of NullIO.
8 9 10 11 12 13 14 15 16 |
# File 'lib/moon-null_io/null_io.rb', line 8 def initialize(fd, mode = 'w+', = {}) @fd = fd @mode = mode = @writable = !!(@mode =~ /[w\+a]/) @readable = !!(@mode =~ /[r\+]/) @binmode = !!(@mode =~ /[b]/) @closed = false end |
Instance Method Details
#<<(*args) ⇒ self
Pushes objects into the stream.
97 98 99 100 |
# File 'lib/moon-null_io/null_io.rb', line 97 def <<(*args) print(*args) self end |
#binmode ⇒ NullIO
Sets the IO into binary mode
59 60 61 |
# File 'lib/moon-null_io/null_io.rb', line 59 def binmode NullIO.new(@fd, @mode + 'b', ) end |
#binmode? ⇒ Boolean
Is the IO in binary mode?
66 67 68 |
# File 'lib/moon-null_io/null_io.rb', line 66 def binmode? @binmode end |
#close ⇒ void
This method returns an undefined value.
Closes the IO
51 52 53 54 |
# File 'lib/moon-null_io/null_io.rb', line 51 def close check_open @closed = true end |
#closed? ⇒ Boolean
Is the IO closed?
21 22 23 |
# File 'lib/moon-null_io/null_io.rb', line 21 def closed? @closed end |
#flush(*args) ⇒ self
Flushes the stream
114 115 116 117 |
# File 'lib/moon-null_io/null_io.rb', line 114 def flush(*args) check_open self end |
#print(*args) ⇒ nil
Writes args to the stream.
83 84 85 86 |
# File 'lib/moon-null_io/null_io.rb', line 83 def print(*args) write(*args) nil end |
#puts(*args) ⇒ nil
Writes args to the stream.
89 90 91 |
# File 'lib/moon-null_io/null_io.rb', line 89 def puts(*args) print(*args) end |
#read ⇒ String
Reads values from the stream, in the case of NullIO, an empty string is returned.
106 107 108 109 |
# File 'lib/moon-null_io/null_io.rb', line 106 def read check_readable '' end |
#write(*args) ⇒ Integer
Writes a string to the stream, in the case of NullIO.
74 75 76 77 |
# 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 |