Forum: Ruby on Rails count number of lines in a file with file.open

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7728d3daa0e59374b1869ef024eb6ee9?d=identicon&s=25 Morgan Morgan (morgan)
on 2008-10-03 23:32
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.
04952a6ee948f345e9c3727850d09a1b?d=identicon&s=25 Dejan Dimic (Guest)
on 2008-10-03 23:52
(Received via mailing list)
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 Morgan <rails-mailing-l...@andreas-s.net>
7728d3daa0e59374b1869ef024eb6ee9?d=identicon&s=25 Morgan Morgan (morgan)
on 2008-10-04 00:12
       @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 Dimic 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 Morgan <rails-mailing-l...@andreas-s.net>
Fb83976abd4d2d2f5f4c4f086a3b09f6?d=identicon&s=25 Raja Venkataraman (rajasaur)
on 2008-10-05 10:47
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.
This topic is locked and can not be replied to.