41

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
    Actually, in order to add an extra newline you need to do `puts "Hello \n\n" since `puts` eats the first newline. I guess it assumes that you don't realize it will automatically add one. – Rob Sobers Jul 10 '11 at 16:57

8 Answers8

60

Just make another call to puts:

puts "Hello"
puts
mipadi
  • 398,885
  • 90
  • 523
  • 479
29
puts "Hello",""
Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134
  • 3
    Clever, but hinders readability. The additional puts is much easier to understand. – Dennis May 14 '14 at 17:14
  • @Dennis I highly disagree, the extra puts almost looks like an error. With this it's arguably much cleaner code. Immediately after putting the string you are putting an empty string which in return is a carriage return. – djowinz Jan 27 '16 at 19:50
  • @djowinz the trouble with this approach is that it's not obvious what it's doing since it's rarely used. The average Ruby programmer does not know that `puts` accepts an array argument and what the result will be. But in any language it's fairly obvious what calling the idiomatic print method twice will do. – Dennis Jan 29 '16 at 16:08
  • @Dennis An average programmer does not know that `puts` accepts an array argument but knows that `puts` with _no_ argument prints a newline? I doubt it. – cozyconemotel Mar 03 '16 at 20:25
  • @cozyconemotel the average Ruby programmer absolutely knows how `puts` behaves without any arguments, especially if they are used to the behaviour of print methods in other common languages. In fact, I gave this code snippet along with an empty `puts` to fellow Rubyists and asked them what they expected the outcome to be. They expected a newline from the empty `puts` and expected the latter to fail because "`puts` only takes one argument". – Dennis Mar 04 '16 at 00:35
  • @Dennis Funny because I gave the same snippets to my novice ruby programmers and asked them to find any flaws and/or typos in them. Many of them pointed out the empty `puts` thinking that it was an typo. No one said anything about multiple arguments passed to `puts`. I guess that made sense to them :p – cozyconemotel Mar 04 '16 at 08:12
  • 1
    @cozyconemotel I've lightened my stance since I've now also gotten responses where they expected `puts` to return an argument error. I guess ultimately `puts "Hello\n"` is the most explicit. ¯\\_(ツ)_/¯ – Dennis Mar 04 '16 at 10:32
6

I often find myself adding a constant in ruby to contain these characters

NEW_LINE = "\n"

puts "Hello" + NEW_LINE

I think it is more readable and makes a change to all newline characters easy if anyone ever decides to separate each line by something else at some later date.

kaybee99
  • 4,566
  • 2
  • 32
  • 42
Anthony
  • 2,411
  • 6
  • 26
  • 23
4

Do you think this looks nicer?


puts "Hello"+$/

</evil>

Geo
  • 93,257
  • 117
  • 344
  • 520
2

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.

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Lennie
  • 10,605
  • 5
  • 22
  • 13
1

you can just write

p "Hello"
p 

That should work as well if you want to keep it short and simple

cjl5108
  • 19
  • 1
  • 1
    `p` will not give a newline. Neither will `p ""`, as it will render `""` because `p` gives the result of calling `.inspect` on the string, instead of `.to_s`. See: http://stackoverflow.com/questions/1255324/p-vs-puts-in-ruby – Magne Jan 13 '17 at 11:43
0

What you want fixed: input for script:

puts "Hello there"
puts "Goodbye"

Output from script:

Hello thereGoodbye

Fix for the problem: Input for script:

puts "Hello there"
puts
puts "Goodbye"

Output from script:

Hello there
Goodbye
Jashaszun
  • 9,207
  • 3
  • 29
  • 57
Nathan
  • 1
  • You should edit your answer and make it easier to understand. – Jacob G Aug 05 '14 at 21:57
  • 1
    Unless I'm misunderstanding something, you've basically said the same thing as the accepted answer that was posted years ago. Is there anything about your answer that adds to the existing one? – skrrgwasme Aug 05 '14 at 22:27
0

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. :)

EBGreen
  • 36,735
  • 12
  • 65
  • 85