Sha256: e8ec218b7bfcd3afc6f781ac46f9f1323d92ed921f830672e63016dc19c6d593

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require "stfu/version"

#
# This gem only contains one function(for now). It will execute the passed
# block and have no output on stdout (or stderr if you choose).
#
module Stfu

  # Silences stdout to /dev/null
  # @param no_stderr Whethor or not to silence stderr as well as stdout
  def self.stfu(silence_stderr=false)
    begin
      orig_stdout = $stdout.clone
      orig_stderr = $stderr.clone unless silence_stderr

      # Windows has different paths for routing to null, perform them here
      if Gem.win_platform?
        $stdout.reopen(File.new('nul', 'w'))
        $stderr.reopen(File.new('nul', 'w')) unless silence_stderr
      else
        $stdout.reopen(File.new('/dev/null', 'w'))
        $stderr.reopen(File.new('/dev/null', 'w')) unless silence_stderr
      end
      ret = yield
    rescue StandardError => e
      $stdout.reopen(orig_stdout)
      $stderr.reopen(orig_stderr) unless silence_stderr
      raise e
    ensure
      $stdout.reopen(orig_stdout)
      $stderr.reopen(orig_stderr) unless silence_stderr
    end
    ret
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stfu-0.1.0 lib/stfu.rb