What is the correct comment line for utf-8 encoding for ruby?

Possible duplicate:
Ruby File Encoding - UTF-8

I use UTF-8 fully and want the ruby ​​interpreter to read my files. Therefore, I put # encoding=utf-8 at the beginning of my ruby ​​code as follows:

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

But from time to time I see other options: bundle gem NAME inserts # -*- encoding: utf-8 -*- (in NAME.gemspec). Pearl magic_encoding also uses this string.

What is the recommended way?

  • # encoding = utf-8
  • # encoding: utf-8
  • # -*- encoding: utf-8 -*-
+4
source share
3 answers

Short answer:

 # encoding: utf-8 

This is the full answer: Ruby File Encoding - UTF-8

+6
source

It seems like a pretty relaxed spec for an acceptable one. I have always used:

 # encoding: UTF-8 

coding also acceptable. I can’t find a link to the allowed options, but as long as your file is interpreted correctly, everything should be fine. Check the __ENCODING__ value to make sure it is raised.

+3
source

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

+3
source

Source: https://habr.com/ru/post/1444605/


All Articles