def wrap(&b) raise "Expected block!" unless block_given? s = "" class << s def write(message) self.<< message end end old = $stdout.clone $stdout = s b.call $stdout = old s end