Glen Glen - 10 days ago 4
Ruby Question

No breaks in the output

Just started Ruby yesterday (for the first time). And struggling a bit. Please help.

Here's the program:

print "What's your name?"
name = gets.chomp
if name == "James"
print "Someone loves you!"
else
print "Try again #{name}!"
end

print "How old are you?"
age = gets.chomp
if age <= "25"
print "Boy, you are just a child"
elsif age >= "45"
print "Shame on you old man, craddle snacher!"
end


The output is:
enter image description here

So my concern is; why is it not beginning from a new line after "Try again Jack". I would like all the questions and answers to start at a fresh line. Please help!

PS: Just ignore the content of the program. That was just something to keep myself motivated. I don't really mean to be offensive.

Answer

2 options, print with explicit linebreaks ( \n , also works on Windows), or puts which adds a linebreak if the string does not already end with one. These two examples result in the same output:

print "Hello\nworld\n"

puts "Hello
world"
Comments