简体   繁体   中英

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.

I am trying to make a simple file counter shown on the stdout while files are being generated. It looks like a rapidly updated number at the same position on the screen.

could use \\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)

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". 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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