I am reading a file which has source code. I need to append 2 spaces before each line. This is what I am doing.
data = read_file
data.split(/\n/).collect {|l| ' ' + l}.join('\n')
However after joining when I do puts then it prints \n literally and it is not a line break. How do I fix that?
You need to use a double quote (
") instead of a single quote. So replace this:with this:
Read more about it here.
You might want to use
\r\ninstead if you want your line-endings to beCRLFinstead ofLF(some Windows editors such as Notepad won’t see aLFlinebreak).