We can also use "\n" ( newline character ) to print a new line whenever we want as used in most of the programming languages.
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 .
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With