Skip to content

Instantly share code, notes, and snippets.

@mycargus
Created October 20, 2017 18:03
Show Gist options
  • Save mycargus/93f0621992ecca1637dda72ebea8f304 to your computer and use it in GitHub Desktop.
Save mycargus/93f0621992ecca1637dda72ebea8f304 to your computer and use it in GitHub Desktop.
Ruby method to suppress output as desired on any platform. Credit: Avdi Grimm
def suppress_output(out: true, err: false)
null = open(File::NULL, "w")
old_out = $stdout.dup
old_err = $stderr.dup
$stdout.reopen(null) if out
$stderr.reopen(null) if err
yield
ensure
$stdout.reopen(old_out)
$stderr.reopen(old_err)
end
puts "Starting up"
suppress_output do
puts "BLAH BLAH BLAH YADDA YADDA LOREM IPSUM PEAS AND CARROTS"
system %(ruby -e 'puts "EVEN MORE BLAH BLAH BLAH"')
end
puts "Shutting down"
# >> Starting up
# >> Shutting down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment