The Many Faces of the Ruby Encoding Line

By Loren Segal on September 23rd, 2010 at 6:54 PM

I just fixed a bug in YARD because it was improperly parsing the encoding line from the top of a source file in Ruby 1.9. YARD needs these encoding lines to convert the encoding before handing the data off to the parser.

You’ve seen these encoding lines before, they usually look like:

# encoding: utf-8

Or, occasionally:

# -*- coding: utf-8 -*-

What you might not have known, however, is that there are in fact much more than a handful of ways in which an encoding line can be written. For your amusement, I present you with the following regression test I wrote for YARD that summarizes the many ways in which you can write out the encoding at the top of a Ruby source file:

Neat, huh.

Questions? Comments? Follow me on Twitter (@lsegal) or email me.