Ruby outputting to the same line as the previous output
Yes, use print var
instead; puts
automatically appends a new line, print
doesn't.
Ruby multiple print on the same line
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)
Writing over previously output lines in the command prompt with ruby
Use \r
to move the cursor to the beginning of the line. And you should not be using puts
as it adds \n
, use print
instead. Like this:
print "11MB 294K/s"
print "\r"
print "12MB 307K/s"
One thing to keep in mind though: \r
doesn't delete anything, it just moves the cursor back, so you would need to pad the output with spaces to overwrite the previous output (in case it was longer).
By default when \n
is printed to the standard output the buffer is flushed. Now you might need to use STDOUT.flush
after print
to make sure the text get printed right away.
output a line to a line before in ruby
f.puts "The result is: #{my_array.flatten.join(" ")}"
Ruby doesn't read carriage return (\r) in output?
puts
will always print a new line at the end, so it's doing your carriage return but then a new line after that. Use print
instead.
90.times { |i| print "#{i} matt lao \r" }
To see it's actually doing the right thing, you can stick in a sleep
:
90.times { |i| print "#{i} matt lao \r"; sleep 0.01 }
Elegantly put strings in same line
I suggest constructing an array and using the join
method.
def printStrings do *strings
strings.join(', ')
end
printStrings 'apple', 'banana', 'orange' #'apple, banana, orange'
Alternatively, you can utilize a special variable, called the output field separator, which you can access as $,
. (Its default value is nil
)
$, = ', '
print 'apple', 'banana', 'orange' #'apple, banana, orange'
$, = nil
This method is outlined in the docs for print
How could I display a text on the same line?
The method gets
without any option receives the input including the terminating end of line. If you simply interpolate that as a part of a string, that end of line will be printed as a line break.
To avoid line break, pass the parameter chomp
with value true
to gets
:
$name = STDIN.gets(chomp: true)
In the old school way, apply chomp
after gets
like:
$name = STDIN.gets.chomp
chomp
removes white space characters from the end of the string, thus it avoids the line break.
Other things wrong about your code are:
- You missed to capitalize the sentence
whats your name?
. - You missed an apostrophe in
whats
. - You should avoid using a global variable like
$name
. Try using another type of variable. - You missed a period at the end of the sentence
Oh #$name, nice to meet you
.
How to overwrite a printed line in the shell with Ruby?
You can use the \r
escape sequence at the end of the line (the next line will overwrite this line). Following your example:
require 'time'
loop do
time = Time.now.to_s + "\r"
print time
$stdout.flush
sleep 1
end
Use repeat of format for many variables on the same line
You could write the following.
def print_my_string(tn, ln, *ints)
fmt = "%-10s" + ("|#{"%2s" % ln}%3d" * ints.size) + "|"
puts fmt % [tn, *ints]
end
Then, for example,
print_my_string("hello", "ho", 2, 77, 453, 61, 999)
displays
hello |ho 2|ho 77|ho453|ho 61|ho999|
after having computed
fmt = "%-10s" + ("|#{"%2s" % ln}%3d" * ints.size) + "|"
#=> %-10s|ho%3d|ho%3d|ho%3d|ho%3d|ho%3d|"
Related Topics
Typeerror: Object Doesn't Support This Property or Method
Fileutils.Mv Throwing Invalid Char \302 and \255 Exception
Ruby or Rails Sort on Two/Multiple Date Fields
Shortening Socket Timeout Using Timeout::Timeout(N) Does Not Seem to Work for Me
Why Does 'Puts(Nil or 4)' Fail in Ruby
Call Next on Ruby Loop from External Method
Remove a Tag But Keep the Text
How to Set Correct Ruby Version in Gem Environment
How to Install "Readline" for Rails Console
Building Gem, Executable Not Found
Git Bash Chcp Windows7 Encoding Issue
Unable to Delete File from Amazon S3 Using Ruby Script
Ruby Regex to Capture Everything Between Two Strings (Inclusive)
How to Create an Operator for Deep Copy/Cloning of Objects in Ruby
How to Upload a JSON File with Secret Keys to Heroku
How to Prevent Rails Controller Generator to Modify Config/Routes.Rb