Module: DarkIO

Defined in:
lib/darkext/io.rb

Class Method Summary collapse

Class Method Details

.capture_output(opts = { }) ⇒ Object

Runs a block and captures the output it generates



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/darkext/io.rb', line 5

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