ruby中的神奇注释(#Encoding:utf-8)是如何工作的? [英] How does the magic comment ( # Encoding: utf-8 ) in ruby​​ work?

查看:43
本文介绍了ruby中的神奇注释(#Encoding:utf-8)是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ruby 中的神奇注释是如何工作的?我说的是:

# 编码:utf-8

这是预处理指令吗?这种结构还有其他用途吗?

解决方案

出于某种原因,人们将此行称为 magic comment.在处理您的源代码解释器之前读取此行并设置正确的编码.我相信这对于解释型语言来说很常见.至少 Python 使用了相同的方法.

您可以通过多种不同的方式指定编码(其中一些可以被编辑器识别):

# 编码:UTF-8# 编码:UTF-8# -*- 编码:UTF-8 -*-

您可以在这篇文章中阅读一些关于源代码编码的有趣内容.>

我所知道的唯一具有类似结构的是 shebang,但它是相关的一般适用于 Unix shell,不是特定于 Ruby 的.

How does the magic comment in ruby​​ works? I am talking about:

# Encoding: utf-8

Is this a preprocessing directive? Are there other uses of this type of construction?

解决方案

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.

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.

这篇关于ruby中的神奇注释(#Encoding:utf-8)是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆