StringIO

H

Hal Fulton

I've been playing with StringIO, and I've found that it doesn't inherit
from IO.

I had thought this would be natural, as it would in theory permit us to
use a StringIO wherever an IO is used.

Comments?

Hal
 
M

Mark Sparshatt

Hal said:
I've been playing with StringIO, and I've found that it doesn't inherit
from IO.

I had thought this would be natural, as it would in theory permit us to
use a StringIO wherever an IO is used.
Well as long as the code doesn't check the class of it's parameters and
just relies on duck typing it should still be possible to use a StringIO
in place of an IO object.


However, IIRC there are parts of the core language that do explicitly
check that they're receiving a string, for speed reasons, so having
StringIO inherit from String makes more sense.
 
W

why the lucky stiff

Hal said:
I've been playing with StringIO, and I've found that it doesn't inherit
from IO.

I had thought this would be natural, as it would in theory permit us to
use a StringIO wherever an IO is used.

I don't know exactly what you're getting at, but, in watching checkins
on Ruby CVS, anything that read:

if obj.is_a? IO

was swapped to:

if obj.respond_to? :read

over the last year. I'm not saying StringIO shouldn't inherit from IO.
Just that it's become less important what descends from IO and more
important what implements obj#read.

_why
 
H

Hal Fulton

why said:
I don't know exactly what you're getting at, but, in watching checkins
on Ruby CVS, anything that read:

if obj.is_a? IO

was swapped to:

if obj.respond_to? :read

over the last year. I'm not saying StringIO shouldn't inherit from IO.
Just that it's become less important what descends from IO and more
important what implements obj#read.

Oh, certainly, I agree.

But I tried to use #select with a StringIO and it complained.
(If this is unreasonable, tell me, but it's not obvious to me.)


Hal
 
H

Hal Fulton

Mark said:
Well as long as the code doesn't check the class of it's parameters and
just relies on duck typing it should still be possible to use a StringIO
in place of an IO object.


However, IIRC there are parts of the core language that do explicitly
check that they're receiving a string, for speed reasons, so having
StringIO inherit from String makes more sense.

I tried a StringIO with #select and it whined at me.

But apparently it doesn't even inherit from String, but from Data:

irb(main):001:0> require "stringio"
=> true
irb(main):002:0> StringIO.ancestors
=> [StringIO, Enumerable, Data, Object, Kernel]



Hal
 
J

Jim Weirich

Hal Fulton said:
But I tried to use #select with a StringIO and it complained.
(If this is unreasonable, tell me, but it's not obvious to me.)

Select is based on the OS select system call which requires file numbers
to work. Since a StringIO object is not connected to an OS file number,
I'm not surprised select didn't work for it.
 
H

Hal Fulton

Jim said:
Hal Fulton said:



Select is based on the OS select system call which requires file numbers
to work. Since a StringIO object is not connected to an OS file number,
I'm not surprised select didn't work for it.

OK, sure, that makes sense. I was thinking purely in terms of
delay -- thinking there would always be a zero wait time.

Thanks,
Hal
 
S

Steven Jenkins

Jim said:
Hal Fulton said:



Select is based on the OS select system call which requires file numbers
to work. Since a StringIO object is not connected to an OS file number,
I'm not surprised select didn't work for it.

Fundamentally, select() means 'block until a read (or write) on this I/O
thing will at least partially succeed'. It would be perfectly consistent
with this meaning to implement select on a StringIO such that it returns
immediately and marks the StringIO object as ready for read and write.

Steve
 
D

daz

Hal said:
I've been playing with StringIO, and I've found that it doesn't inherit
from IO.

I had thought this would be natural, as it would in theory permit us to
use a StringIO wherever an IO is used.

Agree. I ran into it recently in this form
( ruby 1.9.0 (2004-03-21) [i586-bccwin32] ):

#-----
require 'stringio'

sio = StringIO.new
STDOUT.reopen(sio)
#-----

#-> C:/TEMP/rb7162.TMP:5:in `reopen': cannot convert StringIO into IO (TypeError)


I understand Matz is a fan of StringIO.
(I can find references if he wants to argue ;)


daz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top