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?
(Update: You updated your code, so if you're happy working with floats, this is no longer relevant.)gets()
includes the newline. Replace it with gets.strip
.
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
Rspec Any_Instance Deprecation: How to Fix It
Change HTML Form Id Generated by Form_For Rails 3.1
How to Create a Form in Rails Without Having to Use Form_For and a Model Instance
Ruby on Rails Webpacker Can't Find Images Under Asset_Pack_Path
Is It a Bad Idea to Reload Routes Dynamically in Rails
Example of a Prepared Insert Statement Using Ruby Pg Gem
How I Know My Document's Size Inside Mongodb with the Ruby Driver
Whenever Gem: I Set :Output But the Logfile Doesn't Show Up Where I'D Expect It To
Running Phantomjs from a Ruby on Rails Application
Rails 4 Update Nested Attributes
Passing Binding or Arguments to Erb from the Command Line
Trouble Downgrading Ruby on Os X Mavericks
How to Get a Substring from Position N to the Last Char in Ruby