Report abuse

helper define that takes a block

def capturedstdout
   saved = $stdout
   captured = StringIO.new
   $stdout = captured
   begin
      yield
   ensure
      $stdout = saved
   end
   captured.string
end

usage

out = capturedstdout {
   # do your stuff that causes stdout output
}

# now you have STDOUT of the block in 'out' and STDOUT is back to normal