How can I replace new lines with \n character

ruby, ruby-on-rails

Solution

> s = "hi\nthere"
> puts s
hi
there
> puts s.gsub(/\n/, "\\n")
hi\nthere

Problem

Data stored in the database is like this: ``` This is a line This is another line How about this line ``` When I output it to the view, I want to convert that to: ``` This is a line\n\nThis is another line\n\nHow about this line ``` with no new lines and the actual `\n` characters printed out. How can I do that?

Original source