Sha256: 9746f647d56ace3c606f8d680732aa4b17cd0eba6402781382836fac56f698a4

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'stringio'

# a module for suppressing or capturing STDOUT or STDERR.
# useful when shelling out to "noisy" applications or to suppress
# output during tests.
module NoStdout
  module InstanceMethods

    # Suppresses or redirects STDOUT inside the given block.
    # supply an IO of your own to capture STDOUT, otherwise it's put
    # in a new StringIO object.
    def no_stdout ( to = StringIO.new('','r+'), &block )
      orig_stdout  = $stdout
      $stdout      = @alt_stdout = to
      result       = yield
      $stdout      = orig_stdout
      result
    end

    # returns the contents of STDOUT from the previous usage of
    # no_stdout, or nil
    def last_stdout
      return nil unless @alt_stdout
      @alt_stdout.rewind
      @alt_stdout.read
    end

    ## COPIED FROM ABOVE ####

    # Suppresses or redirects STDERR inside the given block.
    # supply an IO of your own to capture STDERR, otherwise it's put
    # in a new StringIO object.
    def no_stderr ( to = StringIO.new('','r+'), &block )
      orig_stderr  = $stderr
      $stderr      = @alt_stderr = to
      result       = yield
      $stderr      = orig_stderr
      result
    end

    # returns the contents of STDERR from the previous usage of
    # no_stderr, or nil
    def last_stderr
      return nil unless @alt_stderr
      @alt_stderr.rewind
      @alt_stderr.read
    end
  end

  def self.included klass
    klass.class_eval do
      include InstanceMethods
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
davidlee-state-fu-0.3.1 lib/no_stdout.rb