在 Ruby on Rails 中,'#encoding: utf-8' 和 'config.encoding = "utf-8"'不同的? [英] In Ruby on Rails, are '#encoding: utf-8' and 'config.encoding = "utf-8"' different?

查看:26
本文介绍了在 Ruby on Rails 中,'#encoding: utf-8' 和 'config.encoding = "utf-8"'不同的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过在顶部添加注释行来指定任何 ruby​​ 文件使用特定编码:

I can specify any ruby file to use specific encoding by add a comment line at its top:

#encoding: utf-8

但是在 Rails 的 config/application.rb 中,我发现了这个:

But in Rails' config/application.rb, I found this:

config.encoding = "utf-8"

它们有什么不同吗?如果我设置了config.encoding = "utf-8",还需要#encoding: utf-8吗?

Are they different? If I have set config.encoding = "utf-8", still I need #encoding: utf-8?

推荐答案

config/application.rb 中的 config.encoding = "utf-8" 部分是相关的Rails 应该如何解释内容.

The config.encoding = "utf-8" part in config/application.rb is related to how rails should interpret content.

#encoding: utf-8 在 ruby​​ 文件中告诉 ruby​​ 这个文件包含非 ascii 字符.

#encoding: utf-8 in a ruby file tells ruby that this file contains non-ascii characters.

这两种情况是不同的.第一个(在 config/application.rb 中)告诉 rails 一些事情,与 ruby​​ 本身应该如何解释源文件完全没有关系.

These two cases are different. The first one (in config/application.rb) tells rails something, and has nothing at all to do with how ruby itself should interpret source files.

你可以设置环境变量RUBYOPT=-Ku,如果你很懒,希望ruby自动将.rb文件的默认文件编码设置为utf-8,但我更愿意建议您将非 ascii 位放在翻译文件中,并使用 I18n.t 引用.

You can set the environment variable RUBYOPT=-Ku if you're lazy and want ruby to automatically set the default file encoding of .rb files to utf-8, but I'd rather recommend that you put your non-ascii bits in a translation file and reference that with I18n.t.

这篇关于在 Ruby on Rails 中,'#encoding: utf-8' 和 'config.encoding = "utf-8"'不同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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