mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
20 lines
378 B
Ruby
20 lines
378 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
require "stringio"
|
||
|
|
||
|
def capture(*args)
|
||
|
opts = args.pop if args.last.is_a?(Hash)
|
||
|
opts ||= {}
|
||
|
|
||
|
args.map!(&:to_s)
|
||
|
begin
|
||
|
result = StringIO.new
|
||
|
result.close if opts[:closed]
|
||
|
args.each {|stream| eval "$#{stream} = result" }
|
||
|
yield
|
||
|
ensure
|
||
|
args.each {|stream| eval("$#{stream} = #{stream.upcase}") }
|
||
|
end
|
||
|
result.string
|
||
|
end
|