Ruby中的魔术评论

Ruby中的魔术评论

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

问题描述

红宝石中的魔术评论如何工作?我在说:

 #编码:utf-8 

这是一个预处理指令吗?这种类型的建筑是否有其他用途?

解决方案

由于某些原因,人们将此行称为 / em>的。在处理您的源代码解释器之前,读取此行并设置正确的编码。我认为这是很常见的解释性语言。至少Python使用相同的方法。



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

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

您可以在。



我知道的唯一的事情有类似的结构是,但它与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)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:23