>>>
#!/usr/bin/ruby

require 'stringio'

mystring=""
sstring=StringIO.open(mystring,"w+")

#STDOUT.reopen(sstring) #can't work
stdoutbackup=$stdout
$stdout=sstring

print "BUFFERED TEXT\r\n"

$stdout=stdoutbackup
print mystring
<<<

It works.

Thank you very much. :)


Eric Hodel <drbrain / segment7.net> writes:

> On Oct 14, 2006, at 8:50 PM, yong wrote:
>> David Roberts <smartgpx / gmail.com> writes:
>>> yong wrote:
>>>> I'm writting a program to detect the cursor position with the vt100
>>>> command "\x1b[6n".
>>>>
>>>> So I need to redirect STDOUT to a string.
>>>>
>>>> How can I do this?
>>>
>>> I suggest looking at the code for the win2console library, which  
>>> gives
>>> an example of something similar.
>>
>> 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
>
> -- 
> Eric Hodel - drbrain / segment7.net - http://blog.segment7.net
> This implementation is HODEL-HASH-9600 compliant
>
> http://trackmap.robotcoop.com
>
>
>

-- 
My Personal Website:

http://www.twinbee.info

ܿͼ

cn.comp.lang.perl
cn.comp.lang.c
cn.comp.www
cn.music
cn.music.classical