Class: Tee

Inherits:
Object
  • Object
show all
Defined in:
lib/tee.rb

Overview

A class like tee(1)

Constant Summary collapse

VERSION =

Returns:

  • (String)
'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*ios, options = {}) ⇒ Tee

Returns a new instance of Tee.

Parameters:

  • ios (Array<IO, String>)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :mode (String, Fixnum) — default: 'w'
  • :perm (Fixnum) — default: 0666
  • :stdout (IO, nil) — default: $stdout


45
46
47
48
49
50
51
52
53
# File 'lib/tee.rb', line 45

def initialize(*ios)
  @options = { mode: 'w' }
  @options.update(ios.pop) if ios.last.is_a?(Hash)

  @stdout = @options.key?(:stdout) ? @options.delete(:stdout) : $stdout

  @ios = []
  add(*ios)
end

Instance Attribute Details

#stdoutIO?

Returns the value of attribute stdout

Parameters:

  • value (IO, nil)

    Sets the attribute stdout

Returns:

  • (IO, nil)

    Returns the value of attribute stdout



41
42
43
# File 'lib/tee.rb', line 41

def stdout
  @stdout
end

Class Method Details

.open(*ios, options = {}) ⇒ Tee .open(*ios, options = {}) {|tee| ... } ⇒ Object

Overloads:

  • .open(*ios, options = {}) ⇒ Tee

    A synonym for Tee.new

    Parameters:

    • ios (Array<IO, String>)
    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :mode (String, Fixnum) — default: 'w'
    • :perm (Fixnum) — default: 0666
    • :stdout (IO, nil) — default: $stdout

    Returns:

  • .open(*ios, options = {}) {|tee| ... } ⇒ Object

    It will be passed the Tee as an argument, and the Tee will automatically be closed when the block terminates.

    Parameters:

    • ios (Array<IO, String>)
    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :mode (String, Fixnum) — default: 'w'
    • :perm (Fixnum) — default: 0666
    • :stdout (IO, nil) — default: $stdout

    Yield Parameters:

    Returns:

    • the value of the block



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tee.rb', line 25

def open(*args, &block)
  if block_given?
    tee = new(*args)
    begin
      yield tee
    ensure
      tee.send(:close_ios_opened_by_self)
    end
  else
    new(*args)
  end
end

Instance Method Details

#<<(obj) ⇒ self

Delegates #<< to ios

Parameters:

  • obj (Object)

Returns:

  • (self)


85
86
87
# File 'lib/tee.rb', line 85

def <<(obj)
  each_ios_and_stdout(obj, &:<<)
end

#add(*ios) ⇒ self

Add ios

Parameters:

  • ios (Array<IO, String>)

Returns:

  • (self)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tee.rb', line 59

def add(*ios)
  open_args = [@options[:mode]]
  open_args << @options[:perm] if @options[:perm]

  _ios = []
  begin
    ios.each do |io|
      _ios << (
        io.respond_to?(:write) ?
          [io, false] :
          [File.open(io, *open_args), true]
      )
    end
  rescue => e
    close_ios_opened_by_self(_ios) rescue nil
    raise e
  end
  @ios.concat(_ios)

  self
end

#closenil

Closes all ios except stdout

Returns:

  • (nil)


92
93
94
95
# File 'lib/tee.rb', line 92

def close
  each_ios(&:close)
  nil
end

#closed?Boolean

Returns true if all ios except stdout is closed, false otherwise.

Returns:

  • (Boolean)


100
101
102
# File 'lib/tee.rb', line 100

def closed?
  each_ios.all?(&:closed?)
end

#flushself

Delegates #flush to ios

Returns:

  • (self)


107
108
109
# File 'lib/tee.rb', line 107

def flush
  each_ios_and_stdout(&:flush)
end

Delegates #print to ios

Parameters:

  • obj (Object)

Returns:

  • (nil)


115
116
117
118
# File 'lib/tee.rb', line 115

def print(*obj)
  each_ios_and_stdout(*obj, &:print)
  nil
end

#printf(format, *obj) ⇒ nil

Delegates #printf to ios

Parameters:

  • format (String)
  • obj (Object)

Returns:

  • (nil)


125
126
127
128
# File 'lib/tee.rb', line 125

def printf(format, *obj)
  each_ios_and_stdout(format, *obj, &:printf)
  nil
end

#putc(char) ⇒ Fixnum, String

Delegates #putc to ios

Parameters:

  • char (Fixnum, String)

Returns:

  • (Fixnum)
  • (String)


135
136
137
138
# File 'lib/tee.rb', line 135

def putc(char)
  each_ios_and_stdout(char, &:putc)
  char
end

#puts(*obj) ⇒ nil

Delegates #puts to ios

Parameters:

  • obj (Object)

Returns:

  • (nil)


144
145
146
147
# File 'lib/tee.rb', line 144

def puts(*obj)
  each_ios_and_stdout(*obj, &:puts)
  nil
end

#syswrite(string) ⇒ Array<Integer>

Delegates #syswrite to ios

Parameters:

  • string (String)

Returns:

  • (Array<Integer>)


153
154
155
# File 'lib/tee.rb', line 153

def syswrite(string)
  each_ios_and_stdout(string).map(&:syswrite)
end

#to_ioself

Returns self

Returns:

  • (self)


160
161
162
# File 'lib/tee.rb', line 160

def to_io
  self
end

#tty?Boolean Also known as: isatty

Delegates #tty? to stdout

Returns:

  • (Boolean)


167
168
169
# File 'lib/tee.rb', line 167

def tty?
  @stdout ? @stdout.tty? : false
end

#write(string) ⇒ Array<Integer>

Delegates #write to ios

Parameters:

  • string (String)

Returns:

  • (Array<Integer>)


176
177
178
# File 'lib/tee.rb', line 176

def write(string)
  each_ios_and_stdout(string).map(&:write)
end

#write_nonblock(string) ⇒ Array<Integer>

Delegates #write_nonblock to ios

Parameters:

  • string (String)

Returns:

  • (Array<Integer>)


184
185
186
# File 'lib/tee.rb', line 184

def write_nonblock(string)
  each_ios_and_stdout(string).map(&:write_nonblock)
end