Reputation:
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
Upvotes: 0
Views: 122
Reputation: 2496
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.
Upvotes: 1
Reputation: 23880
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
Upvotes: 1