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 print something without a new line in ruby
Use print
instead.
You may want to follow it up by STDOUT.flush
.
Ruby - Can I skip a line of input from the console and start reading from the next line?
You can read words like this:
puts 'Number of words:'
words = []
number_of_words = gets.to_i
number_of_words.times do |i|
puts "Word #{i + 1}:"
words << gets.chomp
end
p words
Output:
Number of words:
2
Word 1:
Hacker
Word 2:
Rank
p words
#~> [Hacker", "Rank"]
Remove the newline after variable in `puts()` arguments
gets
will include the newline character in the returned string. To remove that, you can use gets.chomp
.
ROR, ruby on rails console - how to input in new line?
Leave a dangling line of code (ending a line with an operator or a comma)
puts "hello " +
"world"
class Foo
def this_line(is,
unfinished)
end
end
This won't work with MRI Ruby 1.9:
puts "hello
world"
However, a better way of doing this would be:
puts <<-EOF
hello
world
EOF
Ruby Console: How to Keep 'gets' from adding a new line when the user inputs
In windows you can do it like this, otherwise you would need a similar read_char method that works in your OS
def read_char #only on windows
require "Win32API"
Win32API.new("crtdll", "_getch", [], "L").Call
end
def get_number
number, inp = "", 0
while inp != 13
inp = read_char
if "0123456789"[inp.chr]
number += inp.chr
print inp.chr
end
end
number
end
print "Enter the number of boxes: "
boxes = get_number
print " Enter number of columns to print the boxes in: "
columns = get_number
puts ""
puts "boxes: #{boxes}"
puts "columns: #{columns}"
# gives
# Enter the number of boxes: 5 Enter number of columns to print the boxes in: 6
# boxes: 5
# columns: 6
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
My code does not work without that specific line. Why?
When entering the "+" operation, you hit two keys, + and return. Both produce a character, resulting in "+\n"
. (that \n
is a newline character)
chomp
removes the newline character.
Ruby loop create empty lines
You're calling puts x.getName()
, but getName()
already has puts
inside of it.
puts
adds a newline to the end of each argument if there is not one already.
print
does not add a new line.
(What is the difference between print and puts?)
Related Topics
Homebrew Install: Failed During: Git Fetch Origin Master:Refs/Remotes/Origin/Master -N --Depth=1
Ruby: Sum Corresponding Members of Two or More Arrays
How to Set the Default String Encoding on Ruby 1.9
How to Set an Option as Selected Using Selenium Webdriver (Selenium 2.0) Client in Ruby
How to Create an Array in Ruby with Default Values
Ruby Loaderror: Cannot Load Such File
Factorygirl Association Model Trouble: "Systemstackerror: Stack Level Too Deep"
Are There Something Like Python Generators in Ruby
Ruby on Rails Error "Cannot Load Such File -- Less"
How to Convert PDF Files to Images Using Rmagick and Ruby
How to Install Nokogiri Ruby Gem with Mkmf.Log Saying Libiconv Not Found
Rake Assets:Precompile Attempting to Connect to Database
How to Remove a Substring After a Certain Character in a String Using Ruby