priya
priya

Reputation: 26679

How to print something without a new line in ruby

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

Upvotes: 118

Views: 69035

Answers (3)

Atika
Atika

Reputation: 1618

$stdout.sync = true

100.times do
    print "."
    sleep 1
end

"How can I use “puts” to the console without a line break in ruby on rails?"

Upvotes: 7

Rafael Diego Nicoletti
Rafael Diego Nicoletti

Reputation: 463

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

Upvotes: 8

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

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

Upvotes: 148

Related Questions