Sha256: ae0ae4c773f48087ac7908f9c137bf86ef713e619620a5ecab11a0cf15c6e549

Contents?: true

Size: 912 Bytes

Versions: 16

Compression:

Stored size: 912 Bytes

Contents

require 'tempfile'
module Kernel
  # From: 'activesupport-4.1.0/lib/active_support/core_ext/kernel/reporting.rb
  #
  # Captures the given stream and returns it:
  #
  #   stream = capture(:stdout) { puts 'notice' }
  #   stream # => "notice\n"
  #
  #   stream = capture(:stderr) { warn 'error' }
  #   stream # => "error\n"
  #
  # even for subprocesses:
  #
  #   stream = capture(:stdout) { system('echo notice') }
  #   stream # => "notice\n"
  #
  #   stream = capture(:stderr) { system('echo error 1>&2') }
  #   stream # => "error\n"
  def capture(stream)
    stream = stream.to_s
    captured_stream = Tempfile.new(stream)
    stream_io = eval("$#{stream}")
    origin_stream = stream_io.dup
    stream_io.reopen(captured_stream)

    yield

    stream_io.rewind
    return captured_stream.read
  ensure
    captured_stream.close
    captured_stream.unlink
    stream_io.reopen(origin_stream)
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
agile_utils-0.1.1 lib/agile_utils/core_ext/kernel/reporting.rb
agile_utils-0.1.0 lib/agile_utils/core_ext/kernel/reporting.rb
agile_utils-0.0.9 lib/agile_utils/core_ext/kernel/reporting.rb
agile_utils-0.0.8 lib/agile_utils/core_ext/kernel/reporting.rb
agile_utils-0.0.7 lib/agile_utils/core_ext/kernel/reporting.rb
agile_utils-0.0.6 lib/agile_utils/core_ext/kernel/reporting.rb
agile_utils-0.0.5 lib/agile_utils/core_ext/kernel/reporting.rb
agile_utils-0.0.4 lib/agile_utils/core_ext/kernel/reporting.rb
index_html-0.0.7 lib/active_support/core_ext/kernel/reporting.rb
index_html-0.0.6 lib/active_support/core_ext/kernel/reporting.rb
agile_utils-0.0.2 lib/agile_utils/core_ext/kernel/reporting.rb
index_html-0.0.5 lib/active_support/core_ext/kernel/reporting.rb
index_html-0.0.4 lib/active_support/core_ext/kernel/reporting.rb
index_html-0.0.3 lib/active_support/core_ext/kernel/reporting.rb
index_html-0.0.2 lib/active_support/core_ext/kernel/reporting.rb
index_html-0.0.1 lib/active_support/core_ext/kernel/reporting.rb