我可以在Ruby 1.9上设置默认字符串编码吗? [英] Can I set the default string encoding on Ruby 1.9?

查看:218
本文介绍了我可以在Ruby 1.9上设置默认字符串编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来很小,但它一直驱使我坚果。自从上周五在Ruby 1.9上发布应用程序以来,我一直有很多关于字符编码的小例外。几乎所有的都是一些变化:

  Encoding :: CompatibilityError:不兼容的字符编码:ASCII-8BIT和UTF- b $ b  

我们有一个国际用户群,所以很多名字包含umlauts等。如果我修复模板使用 force_encoding 在一堆地方,它弹出在flash消息助手。等等。



目前看来,我已经确定了所有我知道的,通过修补ActiveSupport的字符串连接在一个地方,然后通过设置#encoding:utf-8 在我的每个源文件的顶部。但是感觉,我可能不得不记得为每个Ruby项目的每一个文件做,从现在开始,永远,只是为了避免字符串分配问题,不能坐在我的胃。我阅读了 -Ku 开关,但一切似乎警告,这是为了向后兼容性,可能会随时消失。



for 1.9-experienced folks:is setting #encoding 在我的每个文件中真的有必要吗?是否有合理的方法来做这个全球?或者,更好地,一种方法来设置绕过内部/外部默认值的字符串的非字面值的默认编码?



感谢您提供任何建议。 p>

解决方案

不要将文件编码与字符串编码混淆



文件顶部的 #encoding 语句是让Ruby在读取/解释代码时知道,并且编辑器知道如何处理任何非ASCII字符编辑/读取文件 - 只有在文件中至少有一个非ASCII字符时才需要。例如



要立即在所有文件中定义编码,您可以使用
magic_encoding gem
,它可以向所有ruby插入uft-8魔术评论



运行时遇到的错误 Encoding :: CompatibilityError 是一个



这种情况最有可能在以下情况发生: p>


  • 您正在使用L10N字符串(例如UTF-8),并将它们连接到ASCII字符串(在您的视图中)


  • 用户输入外语字符串(例如UTF-8),并且您的视图尝试打印在某些视图中,以及一些您预定义的固定字符串(ASCII)。 force_encoding 会帮助。在Rails 1.9中还有 Encoding :: primary_encoding 可以设置新字符串的默认编码。
    还有 config.encoding 在/ config / application.rb文件中。


  • 字符串来自数据库,然后结合其他字符串在你的视图。
    (他们的编码可能是绕过,不兼容)。




注意:在创建数据库时请务必指定默认编码!

  create database yourproject_development DEFAULT CHARACTER SET utf8; 

请检查此Yehuda Katz文章并解释得很好:
(特别是一段不兼容的编码)



http://yehudakatz.com/2010/05/05/ruby- 1-9-encodings-a-primer-and-the-solution-for-rails /



http://yehudakatz.com/2010/05/17/encodings-unabridged/



和:



http://zargony.com/2009/07/24/ruby-1-9-and-file-encodings



http://graysoftinc.com/character-encodings


This might sound minor, but it's been driving me nuts. Since releasing an application to production last Friday on Ruby 1.9, I've been having lots of minor exceptions related to character encodings. Almost all of it is some variation on:

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8

We have an international user base so plenty of names contain umlauts, etc. If I fix the templates to use force_encoding in a bunch of places, it pops up in the flash message helper. Et cetera.

At the moment it looks like I've nailed down all the ones I knew about, by patching ActiveSupport's string concatenation in one place and then by setting # encoding: utf-8 at the top of every one of my source files. But the feeling that I might have to remember to do that for every file of every Ruby project I ever do from now on, forever, just to avoid string assignment problems, does not sit well in my stomach. I read about the -Ku switch but everything seems to warn that it's for backwards compatibility and might go away at any time.

So my question for 1.9-experienced folks: is setting #encoding in every one of my files really necessary? Is there a reasonable way to do this globally? Or, better, a way to set the default encoding on non-literal values of strings that bypass the internal/external defaults?

Thanks in advance for any suggestions.

解决方案

Don't confuse file encoding with string encoding

The purpose of the #encoding statement at the top of files is to let Ruby know during reading / interpreting your code, and your editor know how to handle any non-ASCII characters while editing / reading the file -- it is only necessary if you have at least one non-ASCII character in the file. e.g. it's necessary in your config/locale files.

To define the encoding in all your files at once, you can use the magic_encoding gem, it can insert uft-8 magic comment to all ruby files in your app.

The error you're getting at runtime Encoding::CompatibilityError is an error which happens when you try to concatenate two Strings with different encoding during program execution, and their encodings are incompatible.

This most likely happens when:

  • you are using L10N strings (e.g. UTF-8), and concatenate them to e.g. ASCII string (in your view)

  • the user types in a string in a foreign language (e.g. UTF-8), and your view tries to print it out in some view, along with some fixed string which you pre-defined (ASCII). force_encoding will help there. There's also Encoding::primary_encoding in Rails 1.9 to set the default encoding for new Strings. And there is config.encoding in Rails in the config/application.rb file.

  • String which come from your database, and then are combined with other Strings in your view. (their encodings could be either way around, and incompatible).

Side-Note: Make sure to specify a default encoding when you create your database!

    create database yourproject_development  DEFAULT CHARACTER SET utf8;

Please check this Yehuda Katz article, which covers this in-depth, and explains it very well: (there is specifically a section 'Incompatible Encodings')

http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/

http://yehudakatz.com/2010/05/17/encodings-unabridged/

and:

http://zargony.com/2009/07/24/ruby-1-9-and-file-encodings

http://graysoftinc.com/character-encodings

这篇关于我可以在Ruby 1.9上设置默认字符串编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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