ruby .split('\n') not splitting on new line

You need .split("\n"). String interpolation is needed to properly interpret the new line, and double quotes are one way to do that.


Ruby has the methods String#each_line and String#lines

returns an enum: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-each_line

returns an array: http://www.ruby-doc.org/core-2.1.2/String.html#method-i-lines

I didn't test it against your scenario but I bet it will work better than manually choosing the newline chars.


In Ruby single quotes around a string means that escape characters are not interpreted. Unlike in C, where single quotes denote a single character. In this case '\n' is actually equivalent to "\\n".

So if you want to split on \n you need to change your code to use double quotes.

.split("\n")

Tags:

String

Ruby