Ruby 和 Netbeans 问题
我正在一个简单的程序中逐行读取文件,当我将这些行打印到屏幕上时,在 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'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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 puts ,如果行尾还没有换行符,它会附加一个换行符,从而强制刷新缓冲区,效果会更好。
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.我以前从未遇到过这种情况,但我的猜测是您的最后一行没有尾随换行符,因此 Netbeans 控制台不会刷新该行。 尝试添加
$stdout.flush
在程序结束时看看会发生什么。顺便说一句,您可以通过使用
foreach
: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
: