spec/support/kernel.rb
# Captures the given stream and returns it:
#
# @param [String] stream
#
#   The name of the stream to be captured
#
# @example Capture output
#
#   stream = capture(:stdout) { puts 'notice' }
#   stream # => "notice\n"
#
#   stream = capture(:stderr) { warn 'error' }
#   stream # => "error\n"
#
# @example Capture output for subprocesses
#
#   stream = capture(:stdout) { system('echo notice') }
#   stream # => "notice\n"
#
#   stream = capture(:stderr) { system('echo error 1>&2') }
#   stream # => "error\n"
Captures the given stream and returns it.
Captures the given stream and returns it .
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
    captured_stream.read
  ensure
    captured_stream.close
    captured_stream.unlink
    stream_io.reopen(origin_stream)
  end
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 captured_stream . read ensure captured_stream . close captured_stream . unlink stream_io . reopen ( origin_stream ) end
Captures the given stream and returns it:
Captures the given stream and returns it :
# rubocop:disable Security/Eval
stream
<not_specific>
<not_specific>
stream The name of the stream to be captured
stream The name of the stream to be captured
example
Capture output stream = capture(:stdout) { puts 'notice' } stream # => "notice\n" stream = capture(:stderr) { warn 'error' } stream # => "error\n"
Capture output stream = capture ( : stdout ) { puts ' notice ' } stream # = > " notice \ n " stream = capture ( : stderr ) { warn ' error ' } stream # = > " error \ n "
example
Capture output for subprocesses stream = capture(:stdout) { system('echo notice') } stream # => "notice\n" stream = capture(:stderr) { system('echo error 1>&2') } stream # => "error\n"
Capture output for subprocesses stream = capture ( : stdout ) { system ( ' echo notice ' ) } stream # = > " notice \ n " stream = capture ( : stderr ) { system ( ' echo error 1 > &2 ' ) } stream # = > " error \ n "