Created
February 2, 2014 23:38
-
-
Save havenwood/8776768 to your computer and use it in GitHub Desktop.
Capture $stdout for a while.
This file contains 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' | |
def capture | |
old_stdout, $stdout = $stdout, StringIO.new | |
fake_stdout = $stdout | |
yield | |
$stdout = old_stdout | |
fake_stdout.string | |
end | |
capture do | |
puts 'They sailed away, for a year and a day,' | |
puts 'To the land where the Bong-tree grows' | |
end | |
#=> "They sailed away, for a year and a day,\nTo the land where the Bong-tree grows\n" | |
capture do | |
`echo "Alas, do more for subshells..."` | |
end | |
"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment