Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print something without a new line in ruby

Tags:

ruby

puts statement in ruby automatically adds a new line, how do I avoid it?

like image 383
priya Avatar asked Jan 04 '12 06:01

priya


People also ask

How do you print without a new line in Ruby?

Puts automatically adds a new line at the end of your message every time you use it. If you don't want a newline, then use print .

How do you print on the same line in Ruby?

We can also use "\n" ( newline character ) to print a new line whenever we want as used in most of the programming languages.

What is difference between print and puts in Ruby?

While the print method allows you to print information in the same line even multiple times, the puts method adds a new line at the end of the object. On the other hand, p is useful when you are trying to understand what your code does, e.g. when you are trying to figure out a certain error.

How do you leave a line in Ruby?

\r\n should probably do the trick.


2 Answers

Use print instead. You may want to follow it up by STDOUT.flush.

like image 111
Sergio Tulentsev Avatar answered Oct 06 '22 14:10

Sergio Tulentsev


Also, you'll need to append "\r" at end of line to indicate "carriage return" and do next print at beginning of current line

like image 43
Rafael Diego Nicoletti Avatar answered Oct 06 '22 14:10

Rafael Diego Nicoletti