Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - How to keep \n when I print out strings

Tags:

People also ask

How do you print a newline in Ruby?

We can also use "\n" ( newline character ) to print a new line whenever we want as used in most of the programming languages.

Does \n work in a string?

in a string prevents actually making a new line and instead of Type (new line) for a new line it is Type \n for a new line .

What does \n do in Ruby?

In double quoted strings, you can write escape sequences and Ruby will output their translated meaning. A \n becomes a newline. In single quoted strings however, escape sequences are escaped and return their literal definition. A \n remains a \n .

How do I add a new line to a string in Ruby?

You can use whatever you like in the join() method, so if you wanted to have two line breaks between each string, you could do this: puts output. join("\n\n") #=> Hello World, join my game: #=> http://game.com/url #=> Thank You!


I would like to keep \n when I print out strings in ruby,

Like now, if I use puts or print, \n will end up with a newline:

pry(main)> print "abc\nabc"
abc
abc

is there a way to let ruby print it out like: abc\nabc ?

UPDATE

Sorry that maybe I didn't make it more clear. I am debugging my regexps, so when I output a string, if a \n is displayed as a \n, not a newline, it would be easier for me to check. So @slivu 's answer is exactly what I want. Thanks guys.