【发布时间】:2014-08-21 21:37:44
【问题描述】:
我正在寻找一种生成 YAML 文件的方法,避免使用别名(主要是为了简化人类可读性)。我想扩展Psych::Visitors::Emitter 或
Psych::Visitors::Visitor 是要走的路,但我实际上无法找到 Ruby 决定是完全转储锚点还是使用别名引用它。
我什至不介意重复使用锚点(使用它们的 &...... 引用),我只需要将别名扩展为完整结构。
我知道过去有人问过类似的问题,但是:
- Ruby YAML write without aliases 仍未得到答复
- Is it possible to emit valid YAML with anchors / references disabled using Ruby or Python? 给出了 Python 的答案,但没有给出 Ruby 的答案
【问题讨论】:
-
YAML.dump使用YamlTree将您的 Ruby 数据序列化为 Yaml。别名处理由内部Registrar类完成,它只是 emits an alias if the object in question has been seen before(基于object_id)。 -
如果你实现自己的
Visitor没有别名,请注意循环引用!