Class: NullIO

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(fd, mode = 'w+', options = {}) ⇒ NullIO

Returns a new instance of NullIO.

Parameters:

  • fd (Object)

    file descriptor

  • mode (String) (defaults to: 'w+')
  • options (Hash) (defaults to: {})


8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#<<(*args) ⇒ self

Pushes objects into the stream.

Parameters:

  • args (Object)

Returns:

  • (self)


97
98
99
100
# File 'lib/moon-null_io/null_io.rb', line 97

def <<(*args)
  print(*args)
  self
end

#binmodeNullIO

Sets the IO into binary mode

Returns:



59
60
61
# File 'lib/moon-null_io/null_io.rb', line 59

def binmode
  NullIO.new(@fd, @mode + 'b', @options)
end

#binmode?Boolean

Is the IO in binary mode?

Returns:

  • (Boolean)


66
67
68
# File 'lib/moon-null_io/null_io.rb', line 66

def binmode?
  @binmode
end

#closevoid

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?

Returns:

  • (Boolean)


21
22
23
# File 'lib/moon-null_io/null_io.rb', line 21

def closed?
  @closed
end

#flush(*args) ⇒ self

Flushes the stream

Returns:

  • (self)


114
115
116
117
# File 'lib/moon-null_io/null_io.rb', line 114

def flush(*args)
  check_open
  self
end

Writes args to the stream.

Parameters:

  • args (Object)

Returns:

  • (nil)


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.

Parameters:

  • args (Object)

Returns:

  • (nil)


89
90
91
# File 'lib/moon-null_io/null_io.rb', line 89

def puts(*args)
  print(*args)
end

#readString

Reads values from the stream, in the case of NullIO, an empty string is returned.

Returns:

  • (String)

    an empty string



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.

Parameters:

  • args (Object)

Returns:

  • (Integer)

    number of bytes written



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