mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
25 lines
374 B
Ruby
25 lines
374 B
Ruby
|
require 'stringio'
|
||
|
|
||
|
# Mix-in for capturing standard output.
|
||
|
module CaptureStdout
|
||
|
def capture_stdout
|
||
|
s = StringIO.new
|
||
|
oldstdout = $stdout
|
||
|
$stdout = s
|
||
|
yield
|
||
|
s.string
|
||
|
ensure
|
||
|
$stdout = oldstdout
|
||
|
end
|
||
|
|
||
|
def capture_stderr
|
||
|
s = StringIO.new
|
||
|
oldstderr = $stderr
|
||
|
$stderr = s
|
||
|
yield
|
||
|
s.string
|
||
|
ensure
|
||
|
$stderr = oldstderr
|
||
|
end
|
||
|
end
|