Default Encoding For Rails 2

05 Aug 2013

With the release of Ruby 2.0 the default encoding for files is UTF-8. Thus you no longer need to prepend the magic encoding string to every .rb source file

#encoding: UTF-8

However, if you have a project that has to be run under Ruby 1.9.x, adding those magic strings to every file can be really cumbersome. Here’s the trick:

On Linux systems

find . -name "*\.rb" | xargs sed -i "1s/^/#encoding: UTF-8\n\n/"

On OS X systems, the BSD sed version doesn’t behave the same when comes to replace line feeds. Strange, but yes. Read on:

The best solution for me was install GNU sed first:

brew install gnu-sed

And now run:

find . -name "*\.rb" | xargs gsed -i "1s/^/#encoding: UTF-8\n\n/"