From d4fa120671e989eecb71c14647dd2051d28de4a5 Mon Sep 17 00:00:00 2001 From: Krekoten' Marjan Date: Thu, 16 Sep 2010 19:03:55 +0300 Subject: Move capture to Kernel. [#5641 state:resolved] --- .../lib/active_support/core_ext/kernel/reporting.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/kernel/reporting.rb b/activesupport/lib/active_support/core_ext/kernel/reporting.rb index 5105c40e4d..37a827123a 100644 --- a/activesupport/lib/active_support/core_ext/kernel/reporting.rb +++ b/activesupport/lib/active_support/core_ext/kernel/reporting.rb @@ -59,4 +59,23 @@ module Kernel raise unless exception_classes.any? { |cls| e.kind_of?(cls) } end end + + # 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 + alias :silence :capture end -- cgit v1.2.3