4

Possible Duplicate:
Ruby - UTF-8 file encoding

I'm using UTF-8 all the way and want to help the ruby interpreter to read my files. Therefore I put # encoding=utf-8 at the start of my ruby code like this:

#!/usr/bin/env ruby
# encoding=utf-8

But now and then, I see other variants: bundle gem NAME inserts # -*- encoding: utf-8 -*- (into NAME.gemspec). The gem magic_encoding uses this line, too.

What is the recommended way?

  • # encoding = utf-8
  • # encoding: utf-8
  • # -*- encoding: utf-8 -*-
Community
  • 1
  • 1
Johannes
  • 55
  • 4

3 Answers3

6

Short answer:

# encoding: utf-8

This is a complete answer: Ruby - UTF-8 file encoding

Community
  • 1
  • 1
pdjota
  • 3,053
  • 2
  • 22
  • 33
3

It looks like a fairly relaxed specification for what's acceptable. I've always used:

# encoding: UTF-8

Also acceptable is coding. I can't find a reference on the variants allowed, but so long as your file is being interpreted correctly it should be fine. Check the __ENCODING__ value to be sure it's being picked up.

tadman
  • 200,744
  • 21
  • 223
  • 248
3

Python behaves the same way, and the answer to this Python question suggests there is no real recommendation, just choose what works for your editor: for Emacs, use the # -*- encoding: utf-8 -*-; for VIM use # vim:fileencoding=<encoding-name>.

Community
  • 1
  • 1
steenslag
  • 76,334
  • 16
  • 131
  • 165