Skip to content

Instantly share code, notes, and snippets.

@moertel
Last active March 21, 2024 08:54
Show Gist options
  • Select an option

  • Save moertel/11091573 to your computer and use it in GitHub Desktop.

Select an option

Save moertel/11091573 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
@rileytg

rileytg commented Mar 11, 2017

Copy link
Copy Markdown

very nice. i used this for running my sinatra app during some high level cucumber tests. these test used to mix in the sinatra logs with my test output, got very annoying/messy

@iamtheiconoclast

Copy link
Copy Markdown

Fantastic! Thank you, this is exactly what I was looking for. (I also used it to stop output from an inline Sinatra app :)

I believe it can be simplified quite a bit:

  • the begin / end is unnecessary since the rescue and ensure clauses can be hooked onto the method itself (I love Ruby!)
  • the rescue clause is unnecessary since the ensure clause will run anyway before the exception is passed on up the call stack, meaning output will be turned back on before the exception has a chance to get caught (or not) by the calling code
  • the retval is unnecessary since the return value of the method will be the result of yielding to the block anyway
  • which means that retval = is also unnecessary

Putting that all together, it now looks like this:

def suppress_output
  original_stdout, original_stderr = $stdout.clone, $stderr.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

@rebelwarrior

Copy link
Copy Markdown

@nav-mike

Copy link
Copy Markdown

👍 🎉

@robsimpsondev

Copy link
Copy Markdown

Nice one guys! Very useful :)

@gangelo

gangelo commented Jan 31, 2019

Copy link
Copy Markdown

Awesome little ditty.

@nancy-gomez

nancy-gomez commented May 28, 2019

Copy link
Copy Markdown

How can this be edited to instead of just suppressing by sending to dev/null, it instead returned the output so that it can be stored in a variable? Can this be done without redirecting to a file?

@EricDuminil

Copy link
Copy Markdown

@moertel

moertel commented Aug 30, 2019

Copy link
Copy Markdown
Author

@iamtheiconoclast I realise I never looked at the comments here. 😅 Thanks for your suggested simplifications; I've amended the Gist to reflect them.

@Largo

Largo commented Sep 27, 2022

Copy link
Copy Markdown

Windows Version with File::NULL

# 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(File::NULL, 'w'))
  $stdout.reopen(File.new(File::NULL, 'w'))
  yield
ensure
  $stdout.reopen(original_stdout)
  $stderr.reopen(original_stderr)
end

@artur-intech

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment