"Gregory Brown" <gregory.t.brown / gmail.com> wrote/schrieb <b37300880708111035o2af744e1g2411e061054095ee / mail.gmail.com>:

> What problem do you need this for... it sounds like you could get by
> with a different (and better) approach, but we'd need to know a little
> more about what you're trying to do.

Agreed that that there might be a better solution for the original
problem, but sometimes an intermediate problem becomes interesting per
se. Anyway, the original question is how to find a single, more
abstract function replacing these ones:

def with_output_to_string
  $stdout, tmp = StringIO.new, $stdout
  yield
  $stdout, tmp = tmp, $stdout
  tmp.rewind
  tmp.read
end

def with_error_to_string
  $stderr, tmp = StringIO.new, $stderr
  yield
  $stderr, tmp = tmp, $stderr
  tmp.rewind
  tmp.read
end

The only difference is $stdout vs. $stderr, so the idea was to pass
:$stdout resp. :$stderr as parameter.

Regards
  Thomas