Created
May 23, 2014 19:14
-
-
Save havenwood/3370ec8f2e808d81b84a to your computer and use it in GitHub Desktop.
Capture stdout example.
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 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