Created
November 30, 2011 22:29
-
-
Save nesquena/1411419 to your computer and use it in GitHub Desktop.
Capture STDOUT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'stringio' | |
module Kernel | |
# Redirect standard out, standard error | |
# capture_stdout { any_commands; you_want } => "all output from the commands" | |
def capture_stdout | |
out = StringIO.new | |
$stdout = out | |
$stderr = out | |
yield | |
return out.string | |
ensure | |
$stdout = STDOUT | |
$stderr = STDERR | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment