Created
March 19, 2015 14:03
-
-
Save nof/e6a2c59d13fe96a24a8a to your computer and use it in GitHub Desktop.
This file contains 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
require 'logger' | |
class TmpLog | |
def write(message) | |
logger.info(message) | |
end | |
private | |
def logger | |
@logger ||= Logger.new("/tmp/log") | |
end | |
end | |
class Redirect | |
def self.to(out, &block) | |
back_out = $stdout | |
$stdout = out | |
yield | |
ensure | |
$stdout = back_out | |
end | |
end | |
Redirect.to(TmpLog.new) { | |
puts "TO /tmp/log" | |
} | |
Redirect.to(open('/dev/null', 'w')) { | |
puts "TO /dev/null" | |
} | |
puts "TO STDOUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment