# Detecting the end of a text file

**URL:** <https://www.ruby-forum.com/t/detecting-the-end-of-a-text-file/110358>\
**Category:** Ruby\
**Created:** [July 25, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/detecting-the-end-of-a-text-file/110358 "2007-07-25T18:46:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 25, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/detecting-the-end-of-a-text-file/110358/1 "2007-07-25T18:46:39Z")

</div>

I’m just beginning to learn the Ruby progamming language, although I  
have some previous programming experience in Java. I would like to  
know if how I can detect the end of a file when parsing a text file in  
Ruby. I don’t want to throw an error or exception when I reach the end  
of the file, I just want to tell my parser to stop parsing.

Thanks for the help.

The SketchUp Artist

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 25, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/detecting-the-end-of-a-text-file/110358/2 "2007-07-25T18:56:41Z")

</div>

> error or exception when I reach the end of the file, I just  
> want to tell my parser to stop parsing.
> 
> Thanks for the help.
> 
> The SketchUp Artist

File.open(“filename”) do |file|  
while line = file.gets  
puts line  
end  
End

File.open in a block form such as above automatically closes the file  
neatly  
at the end of the block.  
The while loop inside the block assigns the next line from the file to  
the  
variable “line”.  
When there is no next line to read because the end of file has been  
reached,  
the variable will be set to nil. Since the variable, apart from being  
assigned, is also evaluated as part of the condition of the while loop,  
and  
since nil evaluates to false, no more lines will be read once EOF has  
been  
reached.

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 25, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/detecting-the-end-of-a-text-file/110358/3 "2007-07-25T19:03:39Z")

</div>

On 7/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m just beginning to learn the Ruby progamming language, although I  
> have some previous programming experience in Java. I would like to  
> know if how I can detect the end of a file when parsing a text file in  
> Ruby. I don’t want to throw an error or exception when I reach the end  
> of the file, I just want to tell my parser to stop parsing.

As far as I know, all the IO methods return nil on EOF except for  
readline. You could read the file with:

File.open(path) do |f|  
while line=f.gets  
# parse  
end  
end

or

File.open(path) do |f|  
begin  
loop do  
line = f.readline  
# parse  
end  
rescue EOFError  
end

or slurp the whole file with IO.readline(path)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 25, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/detecting-the-end-of-a-text-file/110358/4 "2007-07-25T20:21:01Z")

</div>

On Jul 25, 10:02 am, “Luis P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> loop do
> line = f.readline
> # parse
> end
> 
> ```
> 
> rescue EOFError  
> end
> 
> or slurp the whole file with IO.readline(path)
> 
> –  
> Luis P.[http://ktulu.com.ar/blog/](http://ktulu.com.ar/blog/)

Thanks for the responses Luis. I think I need to read a little more  
about errors and exceptions in Ruby before I use your examples.

Thanks,

The SketchUp Artist
