module Kernel # # Captures the given stream and returns it: # # stream = capture(:stdout) { puts 'Cool' } # stream # => "Cool\n" def capture(stream) begin stream = stream.to_s eval "$#{stream} = StringIO.new" yield result = eval("$#{stream}").string ensure eval("$#{stream} = #{stream.upcase}") end result end end