Alex Rivera | Logout

How can I avoid putting the magic encoding comment on top of every UTF-8 file in Ruby 1.9?

Asked 2010-07-20T14:29:16.803
33

I have a Rails project with a lot of Cyrillic strings in it.

It worked fine on Ruby 1.8, but Ruby 1.9 assumes source files are US-ASCII-encoded unless you provide an # encoding: utf-8 comment at the top of the source file. At that point the files are not considered US-ASCII.

Is there a simpler way to tell Ruby "This application is UTF8-encoded. Please consider all and any included source files as UTF8 unless declared otherwise"?


UPDATE:

I wrote "How to insert the encoding: UTF-8 directive automatically in Ruby 1.9 files" which appends the encoding directive automatically if it's needed.

Edit
Report

3 Answers

13

I think you can either

  1. use -E utf-8 command line argument to ruby, or
  2. set your RUBYOPT environment variable to "-E utf-8"
answered 2010-07-20T15:35:46.290
4

Explicit is better than implicit. Writing out the name of the encoding is good for your text editor, your interpreter, and anyone else who wants to look at the file. Different platforms have different defaults -- UTF-8, Windows-1252, Windows-1251, etc. -- and you will either hamper portability or platform integration if you automatically pick one over the other. Requiring more explicit encodings is a Good Thing.

It might be a good idea to integrate your Rails app with GetText. Then all of your UTF-8 strings will be isolated to a small number of translation files, and your Ruby modules will be clean ASCII.

answered 2010-07-20T14:34:39.643
3

Not a direct answer, but depending on your coding environment you can let the editor take care of things. Emacs' ruby-mode for example has the variable ruby-insert-encoding-magic-comment:

ruby-insert-encoding-magic-comment is a variable defined in `ruby-mode.el' Its value is t

Documentation: *Insert a magic emacs 'coding' comment upon save if this is non-nil.

You can customize this variable.

I'm sure there's something similar for other editors. Sure, it still means adding the magic comment to every file, but at least the editor does it for you automatically instead of you having to remember.

answered 2011-05-18T11:19:22.100

Your Answer