如何避免在 Ruby 1.9 中将神奇的编码注释放在每个 UTF-8 文件的顶部?

发布于 2024-09-10 14:56:13 字数 463 浏览 4 评论 0 原文

我有一个 Rails 项目,其中有很多西里尔字符串。

它在 Ruby 1.8 上运行良好,但 Ruby 1.9 假定源文件是 US-ASCII 编码的,除非您在源文件顶部提供 #encoding: utf-8 注释。此时,这些文件不被视为 US-ASCII

有没有更简单的方法来告诉 Ruby“此应用程序是 UTF8 编码的。除非另有声明,否则请将所有和任何包含的源文件视为 UTF8”?


更新:

我写了“如何在 Ruby 1.9 文件中自动插入编码:UTF-8 指令",如果需要,它会自动附加编码指令。

I have a Rails project with a lot of Cyrillic strings in it.

It worked fine on Ruby 1.8, but Ruby 1.9 assumes source files are US-ASCII-encoded unless you provide an # encoding: utf-8 comment at the top of the source file. At that point the files are not considered US-ASCII.

Is there a simpler way to tell Ruby "This application is UTF8-encoded. Please consider all and any included source files as UTF8 unless declared otherwise"?


UPDATE:

I wrote "How to insert the encoding: UTF-8 directive automatically in Ruby 1.9 files" which appends the encoding directive automatically if it's needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

只是在用心讲痛 2024-09-17 14:56:13

我认为您可以

  1. 使用 -E utf-8 命令行参数来 ruby,或者
  2. 将您的 RUBYOPT 环境变量设置为 "- E utf-8"

I think you can either

  1. use -E utf-8 command line argument to ruby, or
  2. set your RUBYOPT environment variable to "-E utf-8"
笑红尘 2024-09-17 14:56:13

在我看来,显式并不总是比隐式更好。

当您使用的几乎所有源代码都兼容 UTF-8 时,您可以使用 Ruby 的 -Ku 命令行选项轻松避免添加神奇的编码注释。

不要将 -K 选项的“u”参数与 -U 选项混淆。

-Ku : set internal and script encoding to utf-8
-U  : set internal encoding to utf-8

然后,仅在需要它的脚本中设置魔术编码注释。请记住,约定优于配置!

您可以设置环境变量 RUBYOPT=-Ku

请参阅 Ruby 的命令行选项:http:// /www.manpagez.com/man/1/ruby/

In my opinion, explicit is not always better than implicit.

When almost all the source you use is UTF-8 compatible, you can easily avoid putting the magic encoding comment by using Ruby's -Ku command-line options.

Don't confuse the "u" parameter of the -K options with -U options.

-Ku : set internal and script encoding to utf-8
-U  : set internal encoding to utf-8

Then, set the magic encoding comment only in scripts that need it. Remember, convention over configuration!

You can set the environment variable RUBYOPT=-Ku

See Ruby's command-line options at http://www.manpagez.com/man/1/ruby/.

菩提树下叶撕阳。 2024-09-17 14:56:13

显式的比隐式的好。写出编码的名称对于您的文本编辑器、解释器和任何其他想要查看该文件的人都有好处。不同的平台有不同的默认值 - UTF-8、Windows-1252、Windows-1251 等 - 如果您自动选择其中之一,则会妨碍可移植性或平台集成。需要更明确的编码是一件好事。

将 Rails 应用程序与 GetText 集成可能是个好主意。然后,您的所有 UTF-8 字符串将被隔离到少量翻译文件中,并且您的 Ruby 模块将是干净的 ASCII。

Explicit is better than implicit. Writing out the name of the encoding is good for your text editor, your interpreter, and anyone else who wants to look at the file. Different platforms have different defaults -- UTF-8, Windows-1252, Windows-1251, etc. -- and you will either hamper portability or platform integration if you automatically pick one over the other. Requiring more explicit encodings is a Good Thing.

It might be a good idea to integrate your Rails app with GetText. Then all of your UTF-8 strings will be isolated to a small number of translation files, and your Ruby modules will be clean ASCII.

哽咽笑 2024-09-17 14:56:13

有一个 gem 可以在 Rails 项目中需要它的每个文件的顶部设置魔术注释: https:// github.com/m-ryan/magic_encoding

您只需安装它并在项目的根目录中运行 magic_encoding ,问题就解决了。

There's a gem that sets the magic comment on top on every file that needs it in a Rails project : https://github.com/m-ryan/magic_encoding

You just install it and run magic_encoding in the root of your project, problem solved.

很糊涂小朋友 2024-09-17 14:56:13

这不是直接的答案,但根据您的编码环境,您可以让编辑器来处理事情。例如,Emacs 的 ruby-mode 具有变量 ruby-insert-encoding-magic-comment

ruby-insert-encoding-magic-comment 是
在`ruby-mode.el'中定义的变量
其值为t

文档:
*如果不为零,则在保存时插入神奇的 emacs“编码”注释。

您可以自定义此变量。

我确信其他编辑也有类似的事情。当然,这仍然意味着向每个文件添加神奇的注释,但至少编辑器会自动为您完成此操作,而无需您记住。

Not a direct answer, but depending on your coding environment you can let the editor take care of things. Emacs' ruby-mode for example has the variable ruby-insert-encoding-magic-comment:

ruby-insert-encoding-magic-comment is
a variable defined in `ruby-mode.el'
Its value is t

Documentation:
*Insert a magic emacs 'coding' comment upon save if this is non-nil.

You can customize this variable.

I'm sure there's something similar for other editors. Sure, it still means adding the magic comment to every file, but at least the editor does it for you automatically instead of you having to remember.

人疚 2024-09-17 14:56:13

确保所有文件(源文件和资产)在运行时都使用您首选的编码加载的唯一万无一失(且干燥!)的 1.9 方法是使用 -E 命令行参数。

所有其他方法都有缺点,具体取决于您的系统(例如,无法设置 ENV 变量、首先加载的第三方代码导致不适合使用 Encoding.default_external,...)。

我的生产服务器使用以下包装脚本:(

#!/bin/bash
exec /usr/local/rvm/rubies/default/bin/ruby -E utf-8:utf-8 "$@"

确保调整路径)

The only foolproof (and DRY!) 1.9 way of ensuring that all your files (source and assets) are loaded with your preferred encoding at run-time is to use the -E command line argument.

All the other approaches have drawbacks depending on your system (e.g. impossible to set ENV vars, third-party code loaded first making unsuitable to use Encoding.default_external, ...).

My production servers use the following wrapper script:

#!/bin/bash
exec /usr/local/rvm/rubies/default/bin/ruby -E utf-8:utf-8 "$@"

(make sure to adapt the path)

淡淡の花香 2024-09-17 14:56:13

我没有遇到这么多,但是当我需要确保 UTF-8 时,我使用 $KCODE 全局。尝试将其放入您的环境中。rb: $KCODE = 'UTF8'

另外,您确定您的编辑器正在以 UTF-8 保存文件吗?

I don't run into this much, but when I need to ensure UTF-8, I use the $KCODE global. Try putting this in your environment.rb: $KCODE = 'UTF8'

Also, are you certain that your editor is saving files in UTF-8?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文