Ruby - UTF-8 文件编码 [英] Ruby - UTF-8 file encoding

查看:69
本文介绍了Ruby - UTF-8 文件编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道神奇"

# encoding: utf-8

线.但是我已经看到了其他几种替代符号,其中一些非常狂野.你知道或使用这些吗?是否有一些更普遍的可接受的规则?

line. But I've seen several other alternative notations, some of them pretty wild. Do you know or use any of those? Is there some more general rule of what is acceptable?

好的,显然有 3 种方式:

Ok, apparently there are exactly 3 ways:

# encoding: UTF-8

# coding: UTF-8

# -*- coding: UTF-8 -*-

(更多见 http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings)

推荐答案

不,没有正好 3 种方式"来指定神奇注释"——它们有无数种.根据 JEG2:

No, there are not "exactly 3 ways" to specify the 'magic comment' -- there are an infinite number of them. Any comment on the first line that contains coding: will work, according to JEG2:

...设置源编码的首选方式...它被称为魔术注释.如果代码的第一行是包含单词 coding 的注释,后跟冒号和空格,然后是编码名称,则该文件的源编码将更改为指示的编码.

... the preferred way to set your source Encoding ... it's called a magic comment. If the first line of your code is a comment that includes the word coding, followed by a colon and space, and then an Encoding name, the source Encoding for that file is changed to the indicated Encoding.

因此,其中任何一个都应该有效:

So, any of these should work:

# coding: UTF-8
# encoding: UTF-8
# zencoding: UTF-8
# vocoding: UTF-8
# fun coding: UTF-8
# decoding: UTF-8
# 863280148705622662 coding: UTF-8 0072364213
# It was the night before Christmas and all through the house, not a creature was coding: UTF-8, not even with a mouse.

这篇关于Ruby - UTF-8 文件编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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