Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Vikas Kumar vikas027

🏠
Working from home
  • Sydney, Australia
View GitHub Profile
@moertel
moertel / suppress_ruby_output.rb
Last active Feb 2, 2021
Temporarily suppress STDOUT and STDERR (ruby)
View suppress_ruby_output.rb
# 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'))