Skip to content

Instantly share code, notes, and snippets.

@nof
Created March 19, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nof/e6a2c59d13fe96a24a8a to your computer and use it in GitHub Desktop.
Save nof/e6a2c59d13fe96a24a8a to your computer and use it in GitHub Desktop.
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