Class: Tee
- Inherits:
-
Object
- Object
- Tee
- Defined in:
- lib/tee.rb
Overview
A class like tee(1)
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#stdout ⇒ IO?
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(obj) ⇒ self
Delegates #<< to ios.
-
#add(*ios) ⇒ self
Add ios.
-
#close ⇒ nil
Closes all ios except stdout.
-
#closed? ⇒ Boolean
Returns true if all ios except stdout is closed, false otherwise.
-
#flush ⇒ self
Delegates #flush to ios.
-
#initialize(*ios, options = {}) ⇒ Tee
constructor
A new instance of Tee.
-
#print(*obj) ⇒ nil
Delegates #print to ios.
-
#printf(format, *obj) ⇒ nil
Delegates #printf to ios.
-
#putc(char) ⇒ Fixnum, String
Delegates #putc to ios.
-
#puts(*obj) ⇒ nil
Delegates #puts to ios.
-
#syswrite(string) ⇒ Array<Integer>
Delegates #syswrite to ios.
-
#to_io ⇒ self
Returns self.
-
#tty? ⇒ Boolean
(also: #isatty)
Delegates #tty? to stdout.
-
#write(string) ⇒ Array<Integer>
Delegates #write to ios.
-
#write_nonblock(string) ⇒ Array<Integer>
Delegates #write_nonblock to ios.
Constructor Details
#initialize(*ios, options = {}) ⇒ Tee
Returns a new instance of Tee.
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
#stdout ⇒ IO?
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
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
85 86 87 |
# File 'lib/tee.rb', line 85 def <<(obj) each_ios_and_stdout(obj, &:<<) end |
#add(*ios) ⇒ self
Add ios
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 |
#close ⇒ nil
Closes all ios except stdout
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.
100 101 102 |
# File 'lib/tee.rb', line 100 def closed? each_ios.all?(&:closed?) end |
#flush ⇒ self
Delegates #flush to ios
107 108 109 |
# File 'lib/tee.rb', line 107 def flush each_ios_and_stdout(&:flush) end |
#print(*obj) ⇒ nil
Delegates #print to ios
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
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
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
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
153 154 155 |
# File 'lib/tee.rb', line 153 def syswrite(string) each_ios_and_stdout(string).map(&:syswrite) end |
#to_io ⇒ 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
167 168 169 |
# File 'lib/tee.rb', line 167 def tty? @stdout ? @stdout.tty? : false end |
#write(string) ⇒ Array<Integer>
Delegates #write to ios
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
184 185 186 |
# File 'lib/tee.rb', line 184 def write_nonblock(string) each_ios_and_stdout(string).map(&:write_nonblock) end |