Class: Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey-patches/stdout_capture.rb

Overview

Mess wit stdout, capture, restore stdout

Class Method Summary collapse

Class Method Details

.capture(&block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/monkey-patches/stdout_capture.rb', line 8

def self.capture(&block)
  # redirect output to StringIO objects
  stdout = StringIO.new
  stderr = StringIO.new
  $stdout = stdout
  $stderr = stderr

  result = block.call

  # restore normal output
  $stdout = STDOUT
  $stderr = STDERR

  OpenStruct.new result: result, stdout: stdout.string, stderr: stderr.string
end