I want to temporarily capture stderr output.
Im on a freebsd box, so pipe should work.

What am I doing wrong?

--
Simon Strandgaard 


server> ruby x.rb 
Loaded suite TestCapture
Started
F
Finished in 0.023457 seconds.

  1) Failure:
test_capture_stderr(TestCapture) [x.rb:31]:
<"hello world"> expected but was
<"">.

1 tests, 1 assertions, 1 failures, 0 errors
server> cat x.rb 
require 'test/unit'

class TestCapture < Test::Unit::TestCase
  def capture_stderr(&block)
    reader, writer = IO.pipe
    result = ""
    thread = Thread.new do
      loop do
        res = select([reader], nil, nil, nil)[0]
        if res.include?(reader)
          unless reader.eof
            result += reader.read
          end
        end
      end
    end
    old_stderr = $stderr.dup
    $stderr.reopen(writer)
    block.call
    $stderr.flush
    $stderr.reopen(old_stderr)
    thread.kill
    writer.close
    reader.close
    return result
  end
  def test_capture_stderr
    str = capture_stderr {
      $stderr.puts("hello world")
    }
    assert_equal("hello world", str)
  end
end

if $0 == __FILE__
  require 'test/unit/ui/console/testrunner'
  Test::Unit::UI::Console::TestRunner.run(TestCapture)
end
server>