Class: Tee

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

Constant Summary collapse

VERSION =
'0.0.1'

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)
  • :perm (Fixnum)
  • :stdout (IO, nil)


39
40
41
42
43
44
45
46
47
# File 'lib/tee.rb', line 39

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 current value of stdout.

Returns:

  • (IO)

    the current value of stdout



4
5
6
# File 'lib/tee.rb', line 4

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

    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.

    Yield Parameters:

    Returns:

    • the value of the block



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tee.rb', line 17

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

Instance Method Details

#add(*ios) ⇒ self

Add IOs

Parameters:

  • ios (Array<IO,String>)

Returns:

  • (self)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tee.rb', line 53

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_tee(_ios) rescue nil
    raise e
  end
  @ios.concat(_ios)

  self
end

#closenil

Closes all IOs

Returns:

  • (nil)


78
79
80
81
# File 'lib/tee.rb', line 78

def close
  @ios.each { |io,| io.close }
  nil
end

#to_ioself

Returns self

Returns:

  • (self)


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

def to_io
  self
end