From: Christophe Broult Date: 2001-08-29T21:00:05+09:00 Subject: [ruby-talk:20511] Re: File.readline(s) This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C13082.191CBCC0 Content-Type: text/plain; charset="iso-8859-1" > -----Original Message----- > From: Holden Glova [mailto:dsafari@xtra.co.nz] > Sent: Wednesday, August 29, 2001 2:58 AM > To: ruby-talk@ruby-lang.org > Subject: [ruby-talk:20502] Re: File.readline(s) > > On Wed, 29 Aug 2001 05:42, Christophe Broult wrote: > > > -----Original Message----- > > > From: Ryan [mailto:ryan@ryanflynn.com] > > > Sent: Tuesday, August 28, 2001 1:38 PM > > > To: ruby-talk@ruby-lang.org > > > Subject: [ruby-talk:20478] Re: File.readline(s) > > > > > > > def read_f(name) > > > > n = 0 > > > > line = "" > > > > f = File.open(name) > > > > f.each_line{|line| > > > > n += 1 > > > > } > > > > n > > > > end > > > > > > Instead of looping couldn't you just do: > > > > > > puts IO.readlines("c:/file.txt").length > > > > > > ? I'm a newbie, so feel free to correct me if this is not kosher. > > > > IO.readlines load the entire file in memory. So if the file is big > > this is not what you want to do... > > > > Christophe > > Perhaps, but surely that is what the previous code is doing, simple > counting all the lines in the file. I am not sure to understand what you mean. I meant that storing a 1 GB file in memory might not be what you want to do to count the number of line in that file. I agree that using readlines is a simple solution. You have probably read the following solution: > From: Michael Neumann [mailto:neumann@s-direktnet.de] > Sent: Wednesday, August 29, 2001 4:59 AM > To: ruby-talk@ruby-lang.org > Subject: [ruby-talk:20506] Re: File.readline(s) [...] > > This works, too: > > IO.foreach(name){}; $. Christophe ------_=_NextPart_001_01C13082.191CBCC0 Content-Type: text/html; charset="iso-8859-1" RE: [ruby-talk:20502] Re: File.readline(s)

> -----Original Message-----
> From: Holden Glova [mailto:dsafari@xtra.co.nz]
> Sent: Wednesday, August 29, 2001 2:58 AM
> To: ruby-talk@ruby-lang.org
> Subject: [ruby-talk:20502] Re: File.readline(s)
>
> On Wed, 29 Aug 2001 05:42, Christophe Broult wrote:
> > > -----Original Message-----
> > > From: Ryan [mailto:ryan@ryanflynn.com]
> > > Sent: Tuesday, August 28, 2001 1:38 PM
> > > To: ruby-talk@ruby-lang.org
> > > Subject: [ruby-talk:20478] Re: File.readline(s)
> > >
> > > > def read_f(name)
> > > >     n = 0
> > > >     line = ""
> > > >     f = File.open(name)
> > > >     f.each_line{|line|
> > > >         n += 1
> > > >     }
> > > >     n
> > > > end
> > >
> > > Instead of looping couldn't you just do:
> > >
> > > puts IO.readlines("c:/file.txt").length
> > >
> > > ? I'm a newbie, so feel free to correct me if this is not kosher.
> >
> > IO.readlines load the entire file in memory. So if the file is big
> > this is not what you want to do...
> >
> > Christophe
>
> Perhaps, but surely that is what the previous code is doing, simple
> counting all the lines in the file.

I am not sure to understand what you mean. I meant that storing a 1 GB file in memory
might not be what you want to do to count the number of line in that file. I agree that using
readlines is a simple solution. You have probably read the following solution:

> From: Michael Neumann [mailto:neumann@s-direktnet.de]
> Sent: Wednesday, August 29, 2001 4:59 AM
> To: ruby-talk@ruby-lang.org
> Subject: [ruby-talk:20506] Re: File.readline(s)
[...]
>
> This works, too:
>  
>   IO.foreach(name){}; $.

Christophe

------_=_NextPart_001_01C13082.191CBCC0--