Ruby to_yaml utf8 字符串
如何使 ruby to_yaml 方法存储带有原始符号但不带有转义序列的 utf8 字符串?
How can I make ruby to_yaml method to store utf8 strings with original signs but not escape sequence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Ruby 1.9.3+,这不是问题:默认的 YAML 引擎是 Psych,它默认支持 UTF-8。
对于 Ruby 1.9.2 - 您需要安装
psych
gem 并在需要 yaml 之前需要它:或者,按照 Evgeny 的建议设置
yamler
(假设您已经安装了 psych gem):For Ruby 1.9.3+, this is not a problem: the default YAML engine is Psych, which supports UTF-8 by default.
For Ruby 1.9.2- you need to install the
psych
gem and require it before you require yaml:Alternatively, set the
yamler
as Evgeny suggests (assuming you have installed thepsych
gem):在 RubyForge 上查看 Ya2Yaml。
Checkout Ya2Yaml at RubyForge.
这可能是一个非常糟糕的主意,因为我确信 YAML 有其对字符进行编码的原因,但撤消它似乎并不难:
This is probably a really bad idea as I'm sure YAML has its reasons for encoding the characters as it does, but it doesn't seem too hard to undo:
Ruby 附带两个 YAML 引擎:syck 和 psych。 Syck 很旧,没有维护,但在 1.9.2 中它是默认的,因此需要切换到 psych。 Psych 将 UTF-8 字符串转储为 UTF-8。
Ruby ships with two YAML engines: syck and psych. Syck is old and not maintained, but it is default in 1.9.2, so one needs to switch to psych. Psych dumps UTF-8 strings in UTF-8.