File Encoding Magic

Ruby has three default encodings. One of them is the default source encoding, which can be set using a magic comment in the file's first line, or in the second line if the first line is taken by a shebang.

Default source encoding (UTF-8):

p "".encoding #=> #<Encoding:UTF-8>

With encoding comment (file_with_magic_comment.rb):

# coding: cp1252
p "".encoding #=> #<Encoding:Windows-1252>

See Magic Instructions for more information about magic comments in general.

Respecting the Encoding Comment when Reading a File

You might come across a situation, where you want to read in a source file using Ruby's File class, but also want to set the proper encoding from the magic comment. Fortunately, Ruby's standard library supports this. Unfortunately, it is not a stand-alone module, but a part of IRB:

>> require 'irb/magic-file'
# => false
>> IRB::MagicFile
# => #<Object:0x00000001a6bb10>
>> File.open('file_with_magic_comment.rb').read.encoding
# => #<Encoding:UTF-8>
>> IRB::MagicFile.open('file_with_magic_comment.rb').read.encoding
# => #<Encoding:Windows-1252>

Resources

More Idiosyncratic Ruby