ruby 中的神奇注释(#Encoding: utf-8)如何??工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8879237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does the magic comment ( # Encoding: utf-8 ) in ruby?? work?
提问by Leszek Andrukanis
How does the magic comment in ruby?? works? I am talking about:
ruby 中的魔法注释是怎么做的??作品?我说的是:
# Encoding: utf-8
Is this a preprocessing directive? Are there other uses of this type of construction?
这是预处理指令吗?这种结构还有其他用途吗?
回答by KL-7
For some reason people refer to this line as magic comment. Before processing your source code interpreter reads this line and sets proper encoding. It's quite common for interpreted languages I believe. At least Python uses the same approach.
出于某种原因,人们将此行称为魔术注释。在处理您的源代码解释器之前读取此行并设置正确的编码。我相信这对于解释型语言来说很常见。至少 Python 使用了相同的方法。
You can specify encoding in a number of different ways (some of them are recognized by editors):
您可以通过多种不同的方式指定编码(其中一些被编辑器识别):
# encoding: UTF-8
# coding: UTF-8
# -*- coding: UTF-8 -*-
You can read some interesting stuff about source encoding in this article.
您可以在本文中阅读一些有关源代码编码的有趣内容。
The only thing I'm aware of that has similar construction is shebang, but it is related to Unix shells in general and is not Ruby-specific.
我所知道的唯一具有类似结构的是shebang,但它通常与 Unix shell 相关,而不是特定于 Ruby 的。
回答by Holger Just
This magic comment tells Ruby the source encoding of the currently parsed file. As Ruby 1.9.x by default assumes US_ASCIIyou have tell the interpreter what encoding your source code is in if you use non-ASCII characters (like umlauts or accented characters).
这个神奇的注释告诉 Ruby 当前解析文件的源编码。由于 Ruby 1.9.x 默认假设US_ASCII您已经告诉解释器如果您使用非 ASCII 字符(如变音或重音字符),您的源代码采用的是什么编码。
The comment has to be the first line of the file (or below the shebang if used) to be recognized.
注释必须是要识别的文件的第一行(或在shebang 下方,如果使用)。
There are other encoding settings. See this questionfor more information.
还有其他编码设置。有关更多信息,请参阅此问题。
Since version 2.0, Ruby assumes UTF-8 encoding of the source file by default. As such, this magic encoding comment has become a rarer sight in the wild if you write your source code in UTF-8 anyway.
从 2.0 版开始,Ruby 默认假定源文件采用 UTF-8 编码。因此,如果您无论如何都用 UTF-8 编写源代码,那么这种神奇的编码注释在野外就变得很少见了。
回答by J-_-L
As you noted, magic comments are a special preprocessing construct. They must be defined at the top of the file (except, if there is already a unix shebangat the top). As of Ruby 2.3 there are three kinds of magic comments:
正如您所指出的,魔术注释是一种特殊的预处理构造。它们必须在文件的顶部定义(除非顶部已经有一个unix shebang)。从 Ruby 2.3 开始,有 3 种魔法注释:
- Encoding comment: See other answers. Must always be the first magic comment. Must be ASCII compatible. Sets the source encoding, so you will run into problems if the real encoding of the file does not match the specified encoding
frozen_string_literal: true: Freezes all string literals in the current filewarn_indent: true: Activates indentation warnings for the current file
- 编码评论:请参阅其他答案。必须始终是第一个神奇的评论。必须是 ASCII 兼容的。设置源编码,因此如果文件的实际编码与指定的编码不匹配,则会遇到问题
frozen_string_literal: true: 冻结当前文件中的所有字符串文字warn_indent: true: 激活当前文件的缩进警告
More info: Magic Instructions
更多信息:魔术说明
回答by steve
While this isn't exactly an answer for your question, if you want to read more about encodings, how they work, what kinds of problems crop up with them: the great Yehuda Katz wrote about encodings as they were being worked out in Ruby 1.9 and beyond:
虽然这不是您问题的确切答案,但如果您想了解更多关于编码的信息,它们是如何工作的,它们会出现什么样的问题:伟大的 Yehuda Katz 写了关于编码的文章,因为它们正在 Ruby 1.9 中被解决超越:

