Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created February 2, 2014 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/8776768 to your computer and use it in GitHub Desktop.
Save havenwood/8776768 to your computer and use it in GitHub Desktop.
Capture $stdout for a while.
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