123456789_123456789_123456789_123456789_123456789_

Module: Test::Unit::Util::Output

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/test/unit/util/output.rb

Instance Method Summary

  • #capture_output

    Returns output for standard output and standard error as string.

Instance Method Details

#capture_output

Returns output for standard output and standard error as string.

Example:

capture_output do
  puts("stdout")
  warn("stderr")
end # -> ["stdout\n", "stderr\n"]
[ GitHub ]

  
# File 'lib/test/unit/util/output.rb', line 14

def capture_output
  require 'stringio'

  output = StringIO.new
  error = StringIO.new
  stdout_save, stderr_save = $stdout, $stderr
  $stdout, $stderr = output, error
  begin
    yield
    [output.string, error.string]
  ensure
    $stdout, $stderr = stdout_save, stderr_save
  end
end