Question

I'm reading a file line by line in a simple program and when I print the the lines to the screen the last line can't be seen at the ouput window in Netbeans 6.5.1 IDE on Windows XP but when I run the program directly from the command line as "ruby main.rb" there is not a problem (i.e the last line can be seen).I'm using Ruby 1.8.6.Here is the entire code :

File.open("songs.txt","r") do |file|
  file.each do |line|
    print line
   end
end
Was it helpful?

Solution

This will work better if you use puts which will append a newline terminator if there is not already one at the end of the line, forcing a buffer flush.

OTHER TIPS

I've never run across this before myself, but my guess would be that your final line doesn't have a trailing line break, so the Netbeans console isn't flushing the line. Try adding $stdout.flush at the end of the program and see what happens.

By the way, you can simplify this code slightly by rewriting it using foreach:

File.foreach("songs.txt","r") do |file|
  print line
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top