Sha256: 5689b52a5e081e9d0f0dab34c9d78e2f8de5518c3f8d4d5633e13b732620c1e1

Contents?: true

Size: 944 Bytes

Versions: 2

Compression:

Stored size: 944 Bytes

Contents

require 'darkext/hash'

module DarkIO
  # Runs a block and captures the output it generates
  def self.capture_output(opts = { }) # yield e
    opts = { :stdout => true, :stderr => false }.merge(opts)
    cout,cerr = opts[:stdout],opts[:stderr]

    yield and return if !cout && !cerr

    old_stdout = STDOUT.dup if cout
    old_stderr = STDERR.dup if cerr

    r_stdout,w_stdout = IO.pipe if cout
    r_stderr,w_stderr = IO.pipe if cerr

    STDOUT.reopen(w_stdout) if cout
    STDERR.reopen(w_stderr) if cerr

    begin
      yield
    ensure
      STDOUT.reopen(old_stdout) if cout
      STDERR.reopen(old_stderr) if cerr
      w_stdout.close if cout
      w_stderr.close if cerr
    end

    ret_stdout = r_stdout.read if cout
    ret_stderr = r_stderr.read if cerr
    r_stdout.close if cout
    r_stderr.close if cerr
    return ret_stdout,ret_stderr if cout && cerr
    return ret_stdout if cout
    return ret_stderr if cerr
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
darkhelmet-darkext-0.12.0 lib/darkext/io.rb
darkext-0.12.0 lib/darkext/io.rb