From 645de3391276a654ab4a653f5bbe47efc5759b77 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Mon, 7 Nov 2005 00:37:32 +0000 Subject: Added Kernel#silence_stderr to silence stderr for the duration of the given block. Changed Kernel#` to print a message to stderr (like Unix) instead of raising Errno::ENOENT on Win32. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2899 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/kernel.rb | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/kernel.rb b/activesupport/lib/active_support/core_ext/kernel.rb index 168c9cddd3..310bba0904 100644 --- a/activesupport/lib/active_support/core_ext/kernel.rb +++ b/activesupport/lib/active_support/core_ext/kernel.rb @@ -29,6 +29,32 @@ module Kernel $VERBOSE = old_verbose end + # Silences stderr for the duration of the block. + # + # silence_stderr do + # $stderr.puts 'This will never be seen' + # end + # + # $stderr.puts 'But this will' + def silence_stderr + old_stderr = STDERR.dup + STDERR.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') + STDERR.sync = true + yield + ensure + STDERR.reopen(old_stderr) + end + + # Makes backticks behave (somewhat more) similarly on all platforms. + # On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the + # spawned shell prints a message to stderr and sets $?. We emulate + # Unix on the former but not the latter. + def `(command) #:nodoc: + super + rescue Errno::ENOENT => e + STDERR.puts "#$0: #{e}" + end + # Method that requires a library, ensuring that rubygems is loaded def require_library_or_gem(library_name) begin -- cgit v1.2.3