# Count number of lines in a file with file.open

**URL:** <https://www.ruby-forum.com/t/count-number-of-lines-in-a-file-with-file-open/150118>\
**Category:** Rails\
**Created:** [October 3, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/count-number-of-lines-in-a-file-with-file-open/150118 "2008-10-03T23:32:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [October 3, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/count-number-of-lines-in-a-file-with-file-open/150118/1 "2008-10-03T23:32:19Z")

</div>

ok so i’m trying to just read out a file line by line and i can’t seem  
to figure out how to not get an EOFerror… is there some way to tell  
when i get to the end of the file?

i’m just reading out line by line, splitting it and sticking it in a  
database.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [October 3, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/count-number-of-lines-in-a-file-with-file-open/150118/2 "2008-10-03T23:52:41Z")

</div>

file\_out = File.new(“file\_name”, “r”)  
file\_out.each\_line do |line|  
do\_the\_stuf\_vith line  
end

On Oct 3, 11:32 pm, Morgan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [October 5, 2008, 10:47am UTC](https://www.ruby-forum.com/t/count-number-of-lines-in-a-file-with-file-open/150118/3 "2008-10-05T10:47:25Z")

</div>

Hmm, not sure what you are missing, seems fine to me.  
irb(main):001:0\> f = File.new(‘test.txt’)  
=\> #\<File:test.txt\>  
irb(main):002:0\> f.each\_line do |line|  
irb(main):003:1\* w = line.split(’ ')  
irb(main):004:1\> puts w  
irb(main):005:1\> end  
First  
Second  
Third  
=\> #\<File:test.txt\>

My file had First, Second and Third on separate lines.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [October 4, 2008, 12:12am UTC](https://www.ruby-forum.com/t/count-number-of-lines-in-a-file-with-file-open/150118/4 "2008-10-04T00:12:24Z")

</div>

```
   @pid = File.new("filename")
    @pid.each_line do |line|
      @wid = line.split(" ")
      puts @wid
    end

```

am i doing something wrong?.. this just spits out the path to the file  
instead of anything actually in the file.

Dejan D. wrote:

> file\_out = File.new(“file\_name”, “r”)  
> file\_out.each\_line do |line|  
> do\_the\_stuf\_vith line  
> end
> 
> On Oct 3, 11:32ï¿½pm, Morgan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
