Created
April 2, 2009 01:21
-
-
Save davidlee/88977 to your computer and use it in GitHub Desktop.
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 NoStdout | |
module InstanceMethods | |
def no_stdout ( to = StringIO.new('','r+'), &block ) | |
# supply an IO of your own to capture STDOUT, otherwise it's put in a StringIO | |
orig_stdout = $stdout | |
$stdout = @alt_stdout = to | |
result = yield | |
$stdout = orig_stdout | |
result | |
end | |
def last_stdout | |
return nil unless @alt_stdout | |
@alt_stdout.rewind | |
@alt_stdout.read | |
end | |
end | |
# TODO - explain / remember why this has two class_eval blocks - | |
# should one be an extend? | |
def self.included klass | |
klass.class_eval do | |
include InstanceMethods | |
end | |
klass.extend InstanceMethods | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment