简体   繁体   English

Ruby在同一行上多次打印

[英]Ruby multiple print on the same line

Is there an easy way for Ruby to print in one line, then for the next print, it prints to the same line from the beginning. Ruby有一种简单的方法可以在一行中打印,然后在下一次打印时,它从一开始就打印到同一行。

I am trying to make a simple file counter shown on the stdout while files are being generated. 我正在尝试在生成文件时在stdout上显示一个简单的文件计数器。 It looks like a rapidly updated number at the same position on the screen. 它看起来像是屏幕上同一位置的快速更新号码。

could use \\r.. 可以用\\ r ..

while(true) do
  print "\\\r"
  print "|\r"
  print "/\r"
end

Will print the char and then move the cursor back and print over it, making a little spinner like thing. 将打印字符,然后将光标向后移动并打印在它上面,制作一个像旋转器一样的东西。 Else you can look at something like curses.... (https://github.com/rkumar/rbcurse for a ruby wrapper) 否则你可以看看像诅咒......(https://github.com/rkumar/rbcurse为红宝石包装)

You can try something like this: 你可以尝试这样的事情:

print "Hello"
print 13.chr
print "123"

If your console supports line feed it should then replace the first chars and print "123lo". 如果你的控制台支持换行,它应该替换第一个字符并打印“123lo”。 An example with a counter might look as follows: 计数器的示例可能如下所示:

100.times{|x| print x; print 13.chr; sleep 0.01}

If you're printing to a terminal-like thing you can use control characters, or curses. 如果你打印到类似终端的东西,你可以使用控制字符或诅咒。 If you know how many items you're processing it might be easier to just make a textual progress bar, though, like wget/etc. 如果您知道正在处理的项目数量,那么创建文本进度条可能会更容易,例如wget / etc。

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

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