I see.
You need to differentiate a literal backslash followed by a letter n
as a sequence of two characters, and a LF character (a.k.a. newline) that is usually represented as \n
.
You also need to distinguish two different kinds of quoting you're using in Ruby: singles and doubles. Single quotes are literal: the only thing that is interpreted in single quotes specially is the sequence \'
, to escape a single quote, and the sequence \\
, which produces a single backslash. Thus, '\n'
is a two-character string of a backslash and a letter n
.
Double quotes allow for all kinds of weird things in it: you can use interpolation with #{}
, and you can insert special characters by escape sequences: so "\n"
is a string containing the LF control character.
Now, in your database you seem to have the former (backslash and n
), as hinted by two pieces of evidence: the fact that you're seeing literal backslash and n
when you print it, and the fact that gsub
finds a '\n'
. What you need to do is replace the useless backslash-and-n
with the actual line separator characters.
@servicedata.service_exit_details.gsub('\n', "\r\n")