Nick Vanderbilt
Nick Vanderbilt

Reputation: 38450

How do I join by new line

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?

Upvotes: 31

Views: 21574

Answers (2)

TheRedBull
TheRedBull

Reputation: 1

I was able to finally get this to work for my application by using

"<br>"

Upvotes: -14

Veeti
Veeti

Reputation: 5300

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).

Upvotes: 80

Related Questions