def redirect_stdout
      if Capybara::Poltergeist.mri?
        yield
      else
        begin
          prev = STDOUT.dup
          $stdout = @write_io
          STDOUT.reopen(@write_io)
          yield
        ensure
          STDOUT.reopen(prev)
          $stdout = STDOUT
          prev.close
        end
      end
    end
def redirect_stdout
      if Capybara::Poltergeist.mri?
        yield
      else
        begin
          prev = STDOUT.dup
          $stdout = @write_io
          STDOUT.reopen(@write_io)
          yield
        ensure
          STDOUT.reopen(prev)
          $stdout = STDOUT
          prev.close
        end
      end
    end
This abomination is because JRuby doesn't support the :out option of
 Process.spawn. To be honest it works pretty bad with pipes too, because
 we ought close writing end in parent process immediately but JRuby will
 lose all the output from child. Process.popen can be used here and seems
 it works with JRuby but I've experienced strange mistakes on Rubinius.