H
Hidetoshi NAGAI
Hi,
I released a sample of Ruby/Tk: TkTextIO class.
TkTextIO is a class to use a text widget as if an I/O stream
(like as StringIO class).
Because TkTextIO is a subclass of TkText, of course, you can
control it as a text widget.
TkTextIO has same instance methos as IO class's.
You can set a TkTextIO objet to $stdin, $stdout or $stderr.
# When you use 'readline' library, cannot replace $stdin or $stdout.
# If do that, you will get a core dump.
However, there is an important difference.
That is "all operations is done on 'character' base".
For example, TkTextIO#getc returns "one character string".
If the file pointer points a multi-byte character,
you can get the character (not the first byte).
The size of seek or the length of read denotes 'character' size.
For example, seek(10) means "10 characters from head"
(not "10 bytes from head"), and read(20) returns "20 characters"
(not "20 bytes").
By this difference, you cannot replace a stream to a TkTextIO object
when the code assumes that getc returns a Fixnum, and so on.
But I think there are some cases of which the feature is useful.
You can find the source at ext/tk/sample/tktextio.rb on both of
CVS 1.8 and CVS Head.
If you are interested in it, please try it.
I released a sample of Ruby/Tk: TkTextIO class.
TkTextIO is a class to use a text widget as if an I/O stream
(like as StringIO class).
Because TkTextIO is a subclass of TkText, of course, you can
control it as a text widget.
TkTextIO has same instance methos as IO class's.
You can set a TkTextIO objet to $stdin, $stdout or $stderr.
# When you use 'readline' library, cannot replace $stdin or $stdout.
# If do that, you will get a core dump.
However, there is an important difference.
That is "all operations is done on 'character' base".
For example, TkTextIO#getc returns "one character string".
If the file pointer points a multi-byte character,
you can get the character (not the first byte).
The size of seek or the length of read denotes 'character' size.
For example, seek(10) means "10 characters from head"
(not "10 bytes from head"), and read(20) returns "20 characters"
(not "20 bytes").
By this difference, you cannot replace a stream to a TkTextIO object
when the code assumes that getc returns a Fixnum, and so on.
But I think there are some cases of which the feature is useful.
You can find the source at ext/tk/sample/tktextio.rb on both of
CVS 1.8 and CVS Head.
If you are interested in it, please try it.