像其他对象一样将 Mail::Message 序列化到 yaml
问题
普通对象序列化为:
"--- !ruby/object {}\n\n"
而 Mail::Message 序列化为:
"--- \nMime-Version: \"1.0\"\nbody: \"\"\nContent-Transfer-Encoding:[…]"
问题
如何像其他对象一样序列化 Mail::Message? em>
后台
Gem 版本:
- YAML:“0.60”
- 邮件:“2.2.19”
代码
Object.new.to_yaml #gives
"--- !ruby/object {}\n\n"
Mail::Message.new.to_yaml #gives
"--- \nMime-Version: \"1.0\"\nbody: \"\"\nContent-Transfer-Encoding: 7bit\nMessage-ID: <[email protected]>\nsubject: \nContent-Type: text/plain\nDate: Fri, 06 May 2011 15:47:17 +0000\n"
所需输出
"--- !ruby/object:Mail::Message {}\n\n"
Problem
Normal objects serialize to something like:
"--- !ruby/object {}\n\n"
whereas Mail::Message serialize to:
"--- \nMime-Version: \"1.0\"\nbody: \"\"\nContent-Transfer-Encoding:[…]"
Question
How can I have Mail::Message serialized just like other objects?
Background
Gem Versions:
- YAML: "0.60"
- Mail: "2.2.19"
Code
Object.new.to_yaml #gives
"--- !ruby/object {}\n\n"
Mail::Message.new.to_yaml #gives
"--- \nMime-Version: \"1.0\"\nbody: \"\"\nContent-Transfer-Encoding: 7bit\nMessage-ID: <[email protected]>\nsubject: \nContent-Type: text/plain\nDate: Fri, 06 May 2011 15:47:17 +0000\n"
Desired output
"--- !ruby/object:Mail::Message {}\n\n"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原因是邮件宝石的补丁有缺陷。详细信息如下:
https://github.com/mikel/mail/pull/237
The reason was a flawed patch to the Mail gem. Details are outlined here:
https://github.com/mikel/mail/pull/237
由于 Mail::Message 有自己的
to_yaml
方法 - https://github.com/mikel/mail/blob/master/lib/mail/message.rb#L1714 - 我认为没有猴子补丁是不可能的Since Mail::Message has the own
to_yaml
method - https://github.com/mikel/mail/blob/master/lib/mail/message.rb#L1714 - I think it's impossible without monkey-patching like直接使用
YAML
而不是通过to_yaml
方法。Use
YAML
directly instead of going throughto_yaml
method.