Module: Treebis::Capture3

Included in:
FileUtilsProxy, Test::TestCase
Defined in:
lib/treebis.rb

Overview

like Open3 but stay in the ruby runtime

Defined Under Namespace

Classes: StdoutStringIoHack

Instance Method Summary collapse

Instance Method Details

#capture3(&block) ⇒ out_string, ...

Returns:

  • (out_string, err_string, result)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/treebis.rb', line 60

def capture3 &block
  prev_out, prev_err, result = $stdout, $stderr, nil
  out_str, err_str = nil, nil
  $stderr = StringIO.new
  $stdout = StdoutStringIoHack.new
  begin
    result = block.call
  ensure
    $stderr.rewind
    out_str = $stdout.to_str
    err_str = $stderr.read
    $stdout = prev_out
    $stderr = prev_err
  end
  [out_str, err_str, result]
end