Read and write YAML files without destroying anchors and aliases?

后端 未结 3 1906
滥情空心
滥情空心 2020-12-01 16:15

I need to open a YAML file with aliases used inside it:

defaults: &defaults
  foo: bar
  zip: button

node:
  <<: *defaults
  foo: other

3条回答
  •  被撕碎了的回忆
    2020-12-01 16:57

    YAML has aliases and they can round-trip, but you disable it by hash merging. << as a mapping key seems a non-standard extension to YAML (both in 1.8's syck and 1.9's psych).

    require 'rubygems'
    require 'yaml'
    
    yaml = <

    prints

    --- 
    defaults: &id001 
      zip: button
      foo: bar
    node: *id001
    

    but the << in your data merges the aliased hash into a new one which is no longer an alias.

提交回复
热议问题