如何在没有新行的情况下在 ruby 中打印某些内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8723120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print something without a new line in ruby
提问by priya
putsstatement in ruby automatically adds a new line, how do I avoid it?
putsruby 中的语句会自动添加一个新行,我该如何避免呢?
回答by Sergio Tulentsev
Use printinstead.
You may want to follow it up by STDOUT.flush.
使用print来代替。您可能希望通过STDOUT.flush.
回答by Rafael Diego Nicoletti
Also, you'll need to append "\r" at end of line to indicate "carriage return" and do next print at beginning of current line
此外,您需要在行尾附加“\r”以指示“回车”并在当前行的开头进行下一次打印
回答by Atika
$stdout.sync = true
100.times do
print "."
sleep 1
end
"How can I use “puts” to the console without a line break in ruby on rails?"

