简体   繁体   English

Ruby和Netbeans问题

[英]Ruby and Netbeans problem

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 (ie the last line can be seen).I'm using Ruby 1.8.6.Here is the entire code : 我正在用一个简单的程序逐行读取文件,当我将这些行打印到屏幕上时,在Windows XP上的Netbeans 6.5.1 IDE的输出窗口中看不到最后一行,但是当我运行该程序时直接从命令行作为“ ruby​​ main.rb”没有问题(即可以看到最后一行)。我使用的是Ruby 1.8.6。以下是整个代码:

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

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. 我从来没有碰过这个问题,但是我想您的最后一行没有结尾的换行符,因此Netbeans控制台不会刷新该行。 Try adding $stdout.flush at the end of the program and see what happens. 尝试在程序末尾添加$stdout.flush ,看看会发生什么。

By the way, you can simplify this code slightly by rewriting it using foreach : 顺便说一句,您可以使用foreach重写此代码,从而稍微简化一下代码:

File.foreach("songs.txt","r") do |file|
  print line
end

如果您使用puts ,它将更好地工作,如果在行的末尾还没有换行符,则将附加一个换行符,从而强制缓冲区刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM