问题描述
c code code code code在ruby文件中,utf-8 告诉ruby这个文件包含非ASCII字符。
这两种情况是不同的。第一个(在 config / application.rb )告诉rails一些东西,并没有什么关系ruby本身如何解释源文件。
如果你是懒惰的,你可以设置环境变量 RUBYOPT = -Ku ,并希望ruby自动设置默认文件编码 .rb 文件到 utf-8 ,但我宁愿建议将非ASCII码位置放在翻译文件中并引用与 I18n.t 。
I can specify any ruby file to use specific encoding by add a comment line at its top:
#encoding: utf-8But in Rails' config/application.rb, I found this:
config.encoding = "utf-8"Are they different? If I have set config.encoding = "utf-8", still I need #encoding: utf-8?
解决方案The config.encoding = "utf-8" part in config/application.rb is related to how rails should interpret content.
#encoding: utf-8 in a ruby file tells ruby that this file contains non-ascii characters.
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.
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”'不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!