Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created May 23, 2014 19:14
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