How to emit YAML in expanding Ruby aliases

I am looking for a way to emit YAML files, avoiding the use of aliases (mainly to make it easier for people to read). I think the extension Psych::Visitors::Emitteror Psych::Visitors::Visitoris the way to go, but I cannot find where Ruby decides whether to unload the anchor completely or refer to it with an alias.

I would not even mind if the anchors were reused (with their links ...), I just need to extend the aliases to complete structures.

I know similar questions asked in the past, but:

+2
source share
2 answers

The only way I found this is to do a deep clone of the object dumped in YAML. This is because YAML identifies the bindings and aliases based on their identity, and if you cloneor dup, the new object will be equal, but will have a different identification.

There are many ways to do a deep clone, including library support, or write your own helper function - I will leave this as an exercise for the reader.

+1
source

One simple (hacker) approach that I used converted yaml to json. and then convert it back to yaml. The new YAML does not contain aliases / anchors.

require 'json'

jsonObj = oldYaml.to_json
newYaml = YAML.load(jsonObj)
print newYaml.to_yaml
0

All Articles