Handling line endings in Ruby on Rails
Tuesday, February 24th, 2009I’ve been working on a Rails app that automatically builds shell scripts. It pulls a bunch of prewritten code out of an SQLite database and writes it out in order to a text file.
Only problem is there are some rouge line ending sneeking to the database. I’m developing on Solaris 10 where the default line ending is a line feed character (LF, 0x0A). For reasons I don’t understand, Rails and/or SQLite is using an LF CR combination on all of the code that was entered through a text field and saved to the database. When it gets spat back out into the shell script text file and I try to execute it, Korn chokes. Korn expects LF line endings in text files and that’s all there is to it.
After poking around to uncover where the line ending might be being set, I decided it’d be easier to tweak the three places in the Ruby script where code was being outputted to the text file and modify the line ending there.
finalscript << assertion.snippet.code.delete("\x0D") << "\n"
Essentially we’re deleting all of the CR characters from the chunk of code, leaving just the LF which keeps Korn happy.
I’m not totally happy with it as a solution, but it works, it was fast and cheap and solves something that probably doesn’t affect too many people.

