当“to_yaml”时,我们可以强制将文本渲染为二进制吗?

发布于 2024-09-06 09:23:15 字数 594 浏览 4 评论 0原文

requrie 'yaml'
hash = {:title=>'abc'}
hash.to_yaml

将输出:

--- 
title: abc

是否有任何方法可以强制标题为二进制文件,例如:

---
title: !binary|
  5Lit5pa

UPDATE

我问这个是因为我想将数据从数据库转储到 yml 文件。但是数据库中的文本,包含英文和非英文文本,两者都可能有这样的代码:

<% xxx %>

当我使用rake db:fixtures:load时,会出现method xxx之类的错误未找到

在写入文件之前,我可以将 '<%' 替换为 '<%%',但它仅适用于英文文本 - 如果有任何非英文字符,内容将为二进制。当加载回来时,'<%%'仍然是'<%%'。除非我可以强制“to_yaml”始终使用“二进制”作为文本,否则我找不到好的解决方案。

requrie 'yaml'
hash = {:title=>'abc'}
hash.to_yaml

will output:

--- 
title: abc

Is there any method to force the title to be a binary, like:

---
title: !binary|
  5Lit5pa

UPDATE

I'm asking this because I want to dump the data from database to yml files. But the text in the database, contains English and non-English text, and both may have such code:

<% xxx %>

When I use rake db:fixtures:load, there will be an error like method xxx not found.

I can replace '<%' with '<%%' before writing to file, but it's only works for the English text -- If there is any non-English charactors, the content will be binary. When loading back, '<%%' is still '<%%'. I don't find a good solution unless I can force 'to_yaml' always using 'binary' for text.

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

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

发布评论

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

评论(1

心安伴我暖 2024-09-13 09:23:15

简短的回答是“不”。

长的是 - 使用自定义实现覆盖 String#to_yaml (或 Object#to_yaml)方法。

The short answer is 'no'.

The long is - override an String#to_yaml (or Object#to_yaml) method with your custom implementation.

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