Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created November 30, 2011 22:29
Show Gist options
  • Save nesquena/1411419 to your computer and use it in GitHub Desktop.
Save nesquena/1411419 to your computer and use it in GitHub Desktop.
Capture STDOUT
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