Created
October 20, 2017 18:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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