What's the difference between IO and File

Hi

When learning ruby back in 1.6 days, I would see
examples that would use IO and some that would use file.

For example, I would commonly see

lines = IO.readlines(file)

and some that would use File:

File.open(file).each { |line|

}

I don’t know if this is new to 1.8 or not, but I noticed
that I can do

lines = File.open(file).readlines

So, I’m asking myself, if File inherits from IO, why do
I need to use IO.
I think it can be confusing to tell people, “that sometimes
you use IO and sometimes File, but I don’t exactly know
how to tell you when to use what.” :slight_smile:

Thanks

···


Jim Freeze

LSD melts in your mind, not in your hand.

Hi,

One difference between

lines = IO.readlines(file)

and

lines = File.open(file).readlines

is that, I think, IO.readlines is handling closing the file
for you, whereas the latter is relying on the garbage collector
to (eventually) close the file?

Regards,

Bill

In 1.8 you can also do File.read(path) and get back a single String.
I’d like a File.write(path, data) equivalent.

Gavin

···

On Tuesday, April 22, 2003, 4:13:04 AM, Jim wrote:

Hi

When learning ruby back in 1.6 days, I would see
examples that would use IO and some that would use file.

For example, I would commonly see

lines = IO.readlines(file)

and some that would use File:

File.open(file).each { |line|

}

I don’t know if this is new to 1.8 or not, but I noticed
that I can do

lines = File.open(file).readlines

True. But since File inherits from IO, the first example could be written

lines = File.readlines(file)

anyway, which solves the dilemma of “when to use IO and when to use File”.

What’s not clear to me is why IO has a class method which is able to open
files by name. There is no IO.open so why is there IO.readlines? But I see
there is IO.reopen. Hmm, the division of responsibility between IO and File
seems a bit blurred to me…

Regards,

Brian.

···

On Tue, Apr 22, 2003 at 03:39:38AM +0900, Bill Kelly wrote:

Hi,

One difference between

lines = IO.readlines(file)

and

lines = File.open(file).readlines

is that, I think, IO.readlines is handling closing the file
for you, whereas the latter is relying on the garbage collector
to (eventually) close the file?

java has a very non-blurry class division amongst it’s io classes and this is,
IHMO, simply pure evil.

there are alot of things in ruby which, although they may not follow perfect
00 desgin, do follow POLS (relative to other languages!).

take IO.readlines() for instance, now that you mention it, it is an odd method
that may belong in File only, but isn’t IO file based 95% of the time?
perhaps this was the rational… in any case, it seems like a tiny example of
virtual inheritence - where the parent knows of it’s children, and delgates to
one where it sees fit. the more object oriented programming i do, the more
have come to dislike inheritence in the general case; this is a good example
of why inheritence is not good for OO :

  • File knows about the internals of IO, breaking encapsulation
  • IO knows of it’s children, breaking encapsulation

anyways, i would NOT like to see a very clear division of responsibility in
ruby’s io classes if it meant they ended up being closer to java’s. yuck :wink:

-a

btw: i’m not neither agreeing or disagreeing with you - just thought i’d
throw that out there

···

On Tue, 22 Apr 2003, Brian Candler wrote:

What’s not clear to me is why IO has a class method which is able to open
files by name. There is no IO.open so why is there IO.readlines? But I see
there is IO.reopen. Hmm, the division of responsibility between IO and File
seems a bit blurred to me…

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================