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.
We can also use "\n" ( newline character ) to print a new line whenever we want as used in most of the programming languages.
As already mentioned, in ruby the method p can come in handy when you are trying to figure out what a certain line of code does, what's assigned to a variable, or what a method call returns. As it tells you exactly what you are looking at.
Hi, The difference between print and puts is that puts automatically moves the output cursor to the next line (that is, it adds a newline character to start a new line unless the string already ends with a newline), whereas print continues printing text onto the same line as the previous time.
"\n" is newline, '\n\ is literally backslash and n.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With