Class: Cosmos::IoMultiplexer

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/io/io_multiplexer.rb

Overview

Adds IO streams and then defers to the streams when using any of the Ruby output methods such as print, puts, etc.

Direct Known Subclasses

Stderr, Stdout

Instance Method Summary collapse

Constructor Details

#initializeIoMultiplexer

Create the empty stream array



18
19
20
# File 'lib/cosmos/io/io_multiplexer.rb', line 18

def initialize
  @streams = []
end

Instance Method Details

#add_stream(stream) ⇒ Object

Parameters:

  • stream (IO)

    The stream to add



78
79
80
# File 'lib/cosmos/io/io_multiplexer.rb', line 78

def add_stream(stream)
  @streams << stream unless @streams.include?(stream)
end

#flushObject

Calls flush on each stream



51
52
53
# File 'lib/cosmos/io/io_multiplexer.rb', line 51

def flush
  @streams.each {|stream| stream.flush}
end

Parameters:

  • args (Array<String>)

    Argument to send to the print method of each stream



24
25
26
27
# File 'lib/cosmos/io/io_multiplexer.rb', line 24

def print(*args)
  @streams.each {|stream| stream.print(*args)}
  nil
end

#printf(*args) ⇒ Object

Parameters:

  • args (Array<String>)

    Argument to send to the printf method of each stream



31
32
33
34
# File 'lib/cosmos/io/io_multiplexer.rb', line 31

def printf(*args)
  @streams.each {|stream| stream.printf(*args)}
  nil
end

#putc(object) ⇒ Object

Parameters:

  • object (Object)

    Argument to send to the putc method of each stream



38
39
40
41
# File 'lib/cosmos/io/io_multiplexer.rb', line 38

def putc(object)
  @streams.each {|stream| stream.putc(object)}
  object
end

#puts(*args) ⇒ Object

Parameters:

  • args (Array<String>)

    Argument to send to the puts method of each stream



45
46
47
48
# File 'lib/cosmos/io/io_multiplexer.rb', line 45

def puts(*args)
  @streams.each {|stream| stream.puts(*args)}
  nil
end

#remove_default_ioObject

Removes STDOUT and STDERR from the array of streams



72
73
74
75
# File 'lib/cosmos/io/io_multiplexer.rb', line 72

def remove_default_io
  @streams.delete(STDOUT)
  @streams.delete(STDERR)
end

#remove_stream(stream) ⇒ Object

Parameters:

  • stream (IO)

    The stream to remove



83
84
85
# File 'lib/cosmos/io/io_multiplexer.rb', line 83

def remove_stream(stream)
  @streams.delete(stream)
end

#write(string) ⇒ Integer

Returns The length of the string argument.

Parameters:

  • string (String)

    Argument to send to the write method of each stream

Returns:

  • (Integer)

    The length of the string argument



58
59
60
61
# File 'lib/cosmos/io/io_multiplexer.rb', line 58

def write(string)
  @streams.each {|stream| stream.write(string)}
  string.length
end

#write_nonblock(string) ⇒ Integer

Returns The length of the string argument.

Parameters:

  • string (String)

    Argument to send to the write_nonblock method of each stream

Returns:

  • (Integer)

    The length of the string argument



66
67
68
69
# File 'lib/cosmos/io/io_multiplexer.rb', line 66

def write_nonblock(string)
  @streams.each {|stream| stream.write_nonblock(string)}
  string.length
end