helper define that takes a block
1 2 3 4 5 6 7 8 9 10 11 |
saved = $stdout captured = StringIO $stdout = captured begin yield ensure $stdout = saved end captured end |
usage
1 2 3 4 5 |
out = capturedstdout { # do your stuff that causes stdout output } # now you have STDOUT of the block in 'out' and STDOUT is back to normal |

