Skip to content

Instantly share code, notes, and snippets.

@LeipeLeon
Created May 24, 2011 12:55
Show Gist options
  • Save LeipeLeon/988657 to your computer and use it in GitHub Desktop.
Save LeipeLeon/988657 to your computer and use it in GitHub Desktop.
capture std in/out/err
def capture(*streams)
require 'stringio'
streams.map! { |stream| stream.to_s }
begin
result = StringIO.new
streams.each { |stream| eval "$#{stream} = result" }
yield
ensure
streams.each { |stream| eval("$#{stream} = #{stream.upcase}") }
end
result.string
end
capture(:stdout) {
# do your thing
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment