How to Print Something Without a New Line in Ruby

How to print something without a new line in ruby

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

ruby How I could print without leave newline space for each line?

You have two solutions.

The first one uses puts as you currently do:

File.open('yourfile.txt', 'a+') { |f|
f.puts "#{parts[0]}#{parts[1]}#{parts[2]}#{input3}"
}

The second one uses write instead of puts:

File.open('yourfile.txt', 'a+') { |f| 
f.write parts[0]
f.write parts[1]
f.write parts[2]
f.write input3
}

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

You need to use print instead of puts. Also, if you want the dots to appear smoothly, you need to flush the stdout buffer after each print...

def print_and_flush(str)
print str
$stdout.flush
end

100.times do
print_and_flush "."
sleep 1
end

Edit: I was just looking into the reasoning behind flush to answer @rubyprince's comment, and realised this could be cleaned up a little by simply using $stdout.sync = true...

$stdout.sync = true

100.times do
print "."
sleep 1
end

How to do a newline in output

Use "\n" instead of '\n'

Print to log file without newline - Ruby on Rails

The << method lets you write messages without any formatting the log file.

For example:

require 'logger'

log = Logger.new "test.log"
log.info "Start"
5.times { log << "." }
log << "\n"
log.info "End"

Produces:

I, [2014-03-10T15:11:04.320495 #3410]  INFO -- : Start
.....
I, [2014-03-10T15:11:42.157131 #3410] INFO -- : End

Unfortunately, this doesn't let you write to the same line as previous calls to log.info

output a line to a line before in ruby

f.puts "The result is: #{my_array.flatten.join(" ")}"

Ruby: Why is this printed on a new line?

It is printed on the next line because you press ENTER to finish inputing your data (and therefore you make a new line). Didn't it confuse you that second print also goes to a new line?

gets.chomp without moving to a new line

You can do this by using the (very poorly documented) getch:

require 'io/console'
require 'io/wait'

loop do
chars = STDIN.getch
chars << STDIN.getch while STDIN.ready? # Process multi-char paste
break if ["\r", "\n", "\r\n"].include?(chars)
STDOUT.print chars
end

References:

  • http://ruby-doc.org/stdlib-2.1.0/libdoc/io/console/rdoc/IO.html#method-i-getch
  • http://ruby-doc.org/stdlib-2.1.0/libdoc/io/wait/rdoc/IO.html#method-i-ready-3F

Related follow-up question:

enter & IOError: byte oriented read for character buffered IO

Why doesn't puts() print in a single line?

gets() includes the newline. Replace it with gets.strip. (Update: You updated your code, so if you're happy working with floats, this is no longer relevant.)

puts() adds a newline for each argument that doesn't already end in a newline. Your code is equivalent to:

print "#{number1}+#{number2} = ", "\n",
add(number1, number2) , "\n",
"\n"

You can replace puts with print:

print "#{number1}+#{number2} = " , add(number1, number2) , "\n"`

or better:

puts "#{number1}+#{number2} = #{add(number1, number2)}"


Related Topics



Leave a reply



Submit