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:
YAML Anchors and Alias Anchors and Aliases are YAML constructions that allow you to reduce repeat syntax and extend existing data nodes. You can place Anchors ( & ) on an entity to mark a multi-line section. You can then use an Alias ( * ) call that anchor later in the document to reference that section.
Writing configuration files typically involves using the YAML data serialization language. YAML is an abbreviation that is a mark-up language, not a document. It is frequently employed in programs that store or transport data and configuration files. Data serialization is a feature of the YAML module in Ruby.
One simple (hacky) approach I used was convert the yaml to json. and then convert it back to YAML. new YAML does not contain aliases/anchors.
require 'json'
jsonObj = oldYaml.to_json
newYaml = YAML.load(jsonObj)
print newYaml.to_yaml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With