Temporaraly disable Kernel.warn (stderr)

Posted by joakimk, Thu Dec 18 14:02:00 UTC 2008

If you call some code that print warnings to stderr (like Kernel.warn) that can be safetly ignored, you can use this trick to temporaraly turn off stderr:

  $stderr = File.open('/dev/null', 'w')
  code_that_prints_to_stderr
  $stderr = STDERR

Or you cound wrap it in a block:

  def disable_warnings
    $stderr = File.open('/dev/null', 'w')
    yield
    $stderr = STDERR
  end

  disable_warnings do
    code_that_prints_to_stderr
  end

Update: Just learned that if you’re in the context of a rails app you can do:

  silence_warnings do
    code_that_prints_to_stderr
  end

Filed Under: | Tags:

Comments

Have your say

A name is required. You may use HTML in your comments.