Class: Ammeter::OutputCapturer

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

Class Method Summary collapse

Class Method Details

.capture(io, &block) ⇒ Object

Is this thread safe!?!? This won’t work with sub-processes



5
6
7
8
9
10
11
12
13
14
# File 'lib/ammeter/output_capturer.rb', line 5

def self.capture(io, &block)
  case io
  when :stdout
    capture_stdout(&block)
  when :stderr
    capture_stderr(&block)
  else
    raise "Unknown IO #{io}"
  end
end

.capture_stderr(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ammeter/output_capturer.rb', line 28

def self.capture_stderr(&block)
  captured_stream = StringIO.new

  orginal_io, $stderr = $stderr, captured_stream

  block.call

  captured_stream.string
ensure
  $stderr = orginal_io
end

.capture_stdout(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ammeter/output_capturer.rb', line 16

def self.capture_stdout(&block)
  captured_stream = StringIO.new

  orginal_io, $stdout = $stdout, captured_stream

  block.call

  captured_stream.string
ensure
  $stdout = orginal_io
end