Sha256: 6f5e0841a776267702618319c87b64049d00d48ba6d59ac06e15fe67d12aedec

Contents?: true

Size: 938 Bytes

Versions: 55

Compression:

Stored size: 938 Bytes

Contents

# frozen_string_literal: true

module Dsu
  module Services
    # This service captures $stdout, 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
    module StdoutRedirectorService
      class << self
        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_stdout, $stdout = $stdout, string_io # rubocop:disable Style/ParallelAssignment
          yield
          string_io.string
        ensure
          # Restore the original $stdout.
          $stdout = original_stdout
        end
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
dsu-3.0.5 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.4 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.3 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.1 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.beta.3 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.beta.2 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.beta.1 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.beta.0 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.12 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.11 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.10 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.9 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.8 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.7 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.6 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.5 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.4 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.3 lib/dsu/services/stdout_redirector_service.rb
dsu-3.0.0.alpha.2 lib/dsu/services/stdout_redirector_service.rb