How to add extra newline with 'puts' without sticking newline character into string?

Viewed 52828

If I say

puts "Hello"

and decide to add an extra newline I need to do this:

puts "Hello\n"

Having this character in the string is ugly. Is there any way to do this without polluting my string?

8 Answers

Just make another call to puts:

puts "Hello"
puts

Do you think this looks nicer?


puts "Hello"+$/

</evil>

The reason Ruby uses "\n" for a newline is because its based on C. Ruby MRI is written in C and even JRuby is written in Java which is based on C++ which is based on C... you get the idea! So all these C-style languages use the "\n" for the new line.

You can always write your own method that acts like puts but adds new lines based upon a parameter to the method.

Well, I don't think an explicit newline is ugly. mipadi's answer is just fine as well. Just to throw another answer in, make an array of the lines then join the aray with a newline. :)

Related