"W. Kent Starr" <elderburn / mindspring.com> writes:

> On Wed, 17 Jan 2001, Dave Thomas wrote:
> > matz / zetabits.com (Yukihiro Matsumoto) writes:
> > 
> > > OK.  But unlike readlines and each, read is not fundamentally for
> > > reading whole file.  What kind of method do you want?
> > > 
> > >   (a) exactly like:
> > > 
> > >         f = open(path)
> > >         begin
> > >           f.read(*args)
> > >         ensure
> > >           f.close
> > >         end
> > > 
> > >   (b) takes no argument, to read whole file always.
> > > 
> > >   (c) something else.
> > 
> > Hi Matz:
> > 
> > I'd vote for 'a'.
> > 
> 
> Why not 'a' _and 'b' (or does that make it 'c'?)

'a' is a superset of 'b'. THe *args thing means that it calls f.read
with whatever arguments you give it (including no arguments if you
don't supply any). So voting for 'a' gives me 'b' for free!


> f.read(10,30) -:> start at 10, read next 30 ( while !eof if file
> lenght < 30

this is an extension to the File#read instance method. I'm not sure
I've ever needed to read specific line numbers like this. And it's
kinda incompatible with the existing behavior, where the optional
argument is a byte count.

> Also, until you 'learn' it, readlines is a bit confusing; should there not be a
> 'readline' for a line by line read?  Changing behavior would be bad for legacy
> code reasons.

That's already there:

    File.open('dave') do |f|

       f.readline   # reads one line

    end

'readlines' is both a class method and an instance method:

   arr = File.readlines('dave')

and

   File.open('dave') do |f|
     arr = f.readlines
   end

readlines can be a class method because it doesn't need to keep
context: you call it once and it does its business. You couldn't make
'readline' a class method, because there's nowhere to remember the
open file handle between calls.


Regards


Dave