Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created May 23, 2014 19:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/3370ec8f2e808d81b84a to your computer and use it in GitHub Desktop.
Save havenwood/3370ec8f2e808d81b84a to your computer and use it in GitHub Desktop.
Capture stdout example.
require 'stringio'
module Capture
def self.stdout &block
$stdout = captured_stdout = StringIO.new
block.call
captured_stdout
ensure
$stdout = STDOUT
block.call
end
end
captured = Capture.stdout { puts 'toast!' }
#=> #<StringIO:0x007fe0d3adbdb0>
captured.string
#=> "toast!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment