Sha256: 0ea756ddee1de766144cbef677c6a84ab995f70da3d7f7876ec22d0376b08f97

Contents?: true

Size: 941 Bytes

Versions: 19

Compression:

Stored size: 941 Bytes

Contents

require 'darkext/hash'

module DarkIO
  # Runs a block and captures the output it generates
  def self.capture_output(opts = { }) # yield e
    opts.with_defaults!(:stdout => true, :stderr => true)
    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

19 entries across 19 versions & 1 rubygems

Version Path
darkhelmet-darkext-0.5.5 lib/darkext/io.rb
darkhelmet-darkext-0.5.6 lib/darkext/io.rb
darkhelmet-darkext-0.6.0 lib/darkext/io.rb
darkhelmet-darkext-0.7.0 lib/darkext/io.rb
darkhelmet-darkext-0.7.1 lib/darkext/io.rb
darkhelmet-darkext-0.7.2 lib/darkext/io.rb
darkhelmet-darkext-0.7.3 lib/darkext/io.rb
darkhelmet-darkext-0.8.0 lib/darkext/io.rb
darkhelmet-darkext-0.8.1 lib/darkext/io.rb
darkhelmet-darkext-0.8.2 lib/darkext/io.rb
darkhelmet-darkext-0.8.3 lib/darkext/io.rb
darkhelmet-darkext-0.8.4 lib/darkext/io.rb
darkhelmet-darkext-0.8.5 lib/darkext/io.rb
darkhelmet-darkext-0.8.6 lib/darkext/io.rb
darkhelmet-darkext-0.8.7 lib/darkext/io.rb
darkhelmet-darkext-0.9.0 lib/darkext/io.rb
darkhelmet-darkext-0.9.2 lib/darkext/io.rb
darkhelmet-darkext-0.9.3 lib/darkext/io.rb
darkhelmet-darkext-0.9.4 lib/darkext/io.rb