Skip to content

Instantly share code, notes, and snippets.

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 lacostenycoder/56033463fcac9b2a27d17a5c9cb36106 to your computer and use it in GitHub Desktop.
Save lacostenycoder/56033463fcac9b2a27d17a5c9cb36106 to your computer and use it in GitHub Desktop.
Temporarily suppress STDOUT and STDERR (ruby)
# Temporarily redirects STDOUT and STDERR to /dev/null
# but does print exceptions should there occur any.
# Call as:
# suppress_output { puts 'never printed' }
#
def suppress_output
original_stderr = $stderr.clone
original_stdout = $stdout.clone
$stderr.reopen(File.new('/dev/null', 'w'))
$stdout.reopen(File.new('/dev/null', 'w'))
yield
ensure
$stdout.reopen(original_stdout)
$stderr.reopen(original_stderr)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment