Class: STDCapture

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io1, io2) ⇒ STDCapture

Returns a new instance of STDCapture.



5
6
7
8
9
# File 'lib/stdcapture.rb', line 5

def initialize io1,io2
  @io1 = io1
  @io2 = io2
  super ""
end

Class Method Details

.capture(buffer = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stdcapture.rb', line 16

def self.capture buffer = nil
  
  buffer ||= StringIO.new

  stdout_old = $stdout
  stderr_old = $stderr

  $stdout = STDCapture.new(stdout_old,buffer)
  $stderr = STDCapture.new(stderr_old,buffer)

  yield
  
  buffer.string
ensure
  $stdout = stdout_old
  $stderr = stderr_old
end

Instance Method Details

#write(text) ⇒ Object



11
12
13
14
# File 'lib/stdcapture.rb', line 11

def write text
  @io1.write text
  @io2.write text
end