tags:

views:

48

answers:

1

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?

+3  A: 

You need to use a double quote (") instead of a single quote. So replace this:

'\n'

with this:

"\n"

Read more about it here.

You might want to use \r\n instead if you want your line-endings to be CRLF instead of LF (some Windows editors such as Notepad won't see a LF linebreak).

Veeti
Wouldn't `print "\n"` in ruby print `\r\n` on Windows machines, unless you set the output to binary mode?
Andrew Grimm