Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/suppress_output.rb
Overview
This code is taken from gist.github.com/moertel/11091573
Temporarily redirects STDOUT and STDERR to /dev/null but does print exceptions should there occur any. Call as:
suppress_output { puts 'never printed' }
Instance Method Summary collapse
Instance Method Details
#suppress_output ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/suppress_output.rb', line 10 def suppress_output begin original_stderr = $stderr.clone original_stdout = $stdout.clone $stderr.reopen(File.new('/dev/null', 'w')) $stdout.reopen(File.new('/dev/null', 'w')) retval = yield rescue Exception => e $stdout.reopen(original_stdout) $stderr.reopen(original_stderr) raise e ensure $stdout.reopen(original_stdout) $stderr.reopen(original_stderr) end retval end |