On Fri, Jun 16, 2006 at 03:49:25PM +1000, O Plameras wrote:
> Benno wrote:
> >On Fri Jun 16, 2006 at 13:51:23 +1000, O Plameras wrote:
> >  
> >>Matthew Palmer wrote:
> >>
> >>    
> >>>It increases complexity, and unnecessary code will also confuse in the
> >>>future since a programmer will look at that and think "I think that's
> >>>unnecessary, but perhaps there's something special this time that needs 
> >>>it".
> >>>      
> >>It's special this time here:
> >>
> >>#!/usr/bin/env ruby
> >>while not $stdin.eof do
> >> puts $stdin.readline
> >>end
> >>
> >>
> >>and here:
> >>
> >>#!/usr/bin/env ruby
> >>puts $stdin.readline while not $stdin.eof
> >>
> >>    
> >
> >Ok, but isn't that very different to using the $stdin.readlines function?
> 
> In the context of the two examples above,
> 
> $stdin.readline yields the same results as $stdin.readlines.

No.  It.  Does.  Not.  IO#readline provides a single line, while
IO#readlines provides an array containing all of the lines in the file.  The
two methods can *never* return the same value.

Reading the Fine Manual, we discover that your code probably won't work
anyway:

------------------------------------------------------------ IO#readline
     ios.readline(sep_string=$/)   => string

------------------------------------------------------------------------
     Reads a line as with +IO#gets+, but raises an +EOFError+ on end of
     file.
          
So your example would more properly be:

begin
  puts $stdin.readline while true
rescue EOFError
end

Which is an incredibly hideous way to read a file.

> So, as far as the result of my programs above I see no difference. At this
> stage, I do not know if there are any underlying differences. All I know is
> that I get the results I expected.

A very scary way to program.

- Matt
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to