Skip to content

Instantly share code, notes, and snippets.

View vikas027's full-sized avatar
🏠
Working from home

Vikas Kumar vikas027

🏠
Working from home
  • Sydney, Australia
View GitHub Profile
@moertel
moertel / suppress_ruby_output.rb
Last active March 21, 2024 08:54
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'))