如何在Ruby扩展别名中发出YAML [英] How to emit YAML in Ruby expanding aliases

查看:63
本文介绍了如何在Ruby扩展别名中发出YAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种避免使用别名来发出YAML文件的方法(主要是为了简化人类可读性)。我认为扩展 Psych :: Visitors :: Emitter
Psych :: Visitors :: Visitor 是可行的方法,但是我实际上找不到Ruby决定要完全抛弃锚还是使用别名引用锚的地方。

I am looking for a way to emit YAML files avoiding the use of aliases (mostly for simplified human readability). I think extending Psych::Visitors::Emitter or Psych::Visitors::Visitor is the way to go, but I cannot actually find where Ruby decides whether to dump an anchor in full, or reference it with an alias.

我什至都不介意锚被重复使用(带有& ......引用),我只需要将别名扩展到完整结构。

I wouldn't even mind if the anchors were used repeatedly (with their &...... references), I just need to expand aliases to the full structures.

我知道类似的问题在过去被问到,但是:

I am aware of similar questions being asked in the past, but:

  • Ruby YAML write without aliases remained unanswered
  • Is it possible to emit valid YAML with anchors / references disabled using Ruby or Python? gave answer for Python but not for Ruby

推荐答案

唯一的方法我发现这样做是对转储到YAML的对象进行深层克隆。这是因为YAML将根据锚和别名的身份来识别锚和别名,如果您 clone dup ,新对象将是相等的,但具有不同的标识。

The only way I've found to do this is to perform a deep clone of the object being dumped to YAML. This is because YAML will identify the anchors and aliases based on their identity, and if you clone or dup them, the new object will be equal, but have a different identity.

执行深度克隆的方法有很多,包括库支持或编写自己的帮助函数-我'将其留给读者练习。

There are many ways to perform a deep clone, including library support, or writing your own helper function -- I'll leave that as an exercise for the reader.

这篇关于如何在Ruby扩展别名中发出YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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