Sha256: f0c9f62e23b733556a82c60e00304afad93b0125da128a88d7e899d09b30b0f0

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

=begin rdoc

= OutputCatcher

by Matthias Hennemeyer <mhennemeyer@gmail.com>

== Introduction

OutputCatcher is available as a Rails plugin and as a gem.
It provides a way to capture the standard out($stdout) or standard error($stderr) of your code without pain
and suppresses the output of the 'err' or 'out' stream.


== Usage

  OutputCatcher knows only two methods: .catch_err and .catch_out

  To capture the stderr of your code:

  err = OutputCatcher.catch_err do
    $stderr << "error error"
  end
  err #=> "error error"


  To capture the stdout of your code:

  out = OutputCatcher.catch_out do
    puts "Hello Hello"
  end
  out #=> "Hello Hello"

== INSTALL:
Rails:

  $ ruby script/plugin install git://github.com/mhennemeyer/output_catcher.git

Gem:

  $ gem install mhennemeyer-output_catcher


Copyright (c) 2008 Matthias Hennemeyer, released under the MIT license

=end

require 'stringio'

class OutputCatcher
  class << self
    
    def catch_io(post, &block)
      original = eval("$std" + post)
      fake = StringIO.new
      eval("$std#{post} = fake")
      begin
        yield
      ensure
        eval("$std#{post} = original")
      end
      fake.string
    end
    
    def catch_out(&block)
      catch_io("out", &block)
    end
    
    def catch_err(&block)
      catch_io("err", &block)
    end
    
  end
end

class Tee < IO
  FILE_DESC = {:in => 0, :out => 1, :err => 2}
  
  def initialize(filename, post = :out, mode_string = 'w')
    super(FILE_DESC[post], mode_string)
    @file = File.open(filename, mode_string)
    eval("$std#{post} = self")
  end
  
  def write(string)
    STDOUT.write string
    @file.puts string
    @file.flush
  end
  
  def self.open(filename, post, mode_string, &block)
    
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rpipe-0.1.7 vendor/output_catcher.rb
rpipe-0.1.6 vendor/output_catcher.rb
rpipe-0.1.4 vendor/output_catcher.rb
rpipe-0.1.3 vendor/output_catcher.rb
rpipe-0.1.2 vendor/output_catcher.rb
rpipe-0.1.1 vendor/output_catcher.rb
rpipe-0.1.0 vendor/output_catcher.rb