Module: Dsu::Services::StderrRedirectorService

Defined in:
lib/dsu/services/stderr_redirector_service.rb

Overview

This service captures $stderr, resirects it to a StringIO object, and returns the string value. https://stackoverflow.com/questions/4459330/how-do-i-temporarily-redirect-stderr-in-ruby/4459463#4459463

Class Method Summary collapse

Class Method Details

.call ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dsu/services/stderr_redirector_service.rb', line 10

def call
  raise ArgumentError, 'no block was provided' unless block_given?

  # The output stream must be an IO-like object. In this case we capture it in
  # an in-memory IO object so we can return the string value. Any IO object can
  # be used here.
  string_io = StringIO.new
  original_stderr, $stderr = $stderr, string_io # rubocop:disable Style/ParallelAssignment
  yield
  string_io.string
ensure
  # Restore the original $stderr.
  $stderr = original_stderr
end