On Oct 15, 2006, at 3:30 AM, yong wrote: > Eric Hodel <drbrain / segment7.net> writes: >> On Oct 14, 2006, at 8:50 PM, yong wrote: >>> Now I can use STDOUT.reopen to redirect STDOUT output to a file.But >>> How can I redirect it to a string? >> >> You don't. >> >> You should avoid touching STDOUT and STDERR as much as possible. >> >> Instead assign a StringIO to $stdout. >> >> http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout > > require 'stringio' > > mystring="" > sstring=StringIO.open(mystring,"w+") sstring = StringIO.new will suffice. > #STDOUT.reopen(sstring) #can't work > stdoutbackup=$stdout > $stdout=sstring > > print "BUFFERED TEXT\r\n" > > $stdout=stdoutbackup I tend to wrap this up in a method that yields. See util_capture in ZenTest's test/zentest_assertions.rb -- Eric Hodel - drbrain / segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com