1

Possible Duplicate:
Read and write YAML files without destroying anchors and aliases?

I am using ruby 1.9.3 with default yaml engine to modify yaml files on the fly within the app like this:

require 'yaml'

langs = YAML.load_file("i18n/de.yml")

langs["key"] = 'val' # change key to new value val

File.open("i18n/de.yml", 'w') do |f|
    YAML.dump(langs,f)
end

This works very well, but I have a problem with aliases in the yml. So assume de.yml is this:

---
main: &aliasName
  key:
    title: translation
  another_key:
    title: another translation

something:
  <<: *aliasName

After calling the script like above, this get's translated to:

---
main:
  key: &18803600
    title: translation
  another_key: &18803120
    title: another translation
something:
  key: *18803600
  another_key: *18803120
key: val

If I now add something to main by editing the file by hand, for example main.third_key, it won't get aliased in something since the alias was converted to explicitly only alias main.key and main.another_key between main and something.

So it looks like the aliases get dereferenced on YAML.dump or YAML.load. Is there any way to save the aliases like they are defined in the yaml file? de.yml might look like this (I don't care if the alias name changes):

---
main: &18803600
  key: 
    title: translation
  another_key:
    title: another translation
something:
  <<: *18803600
key: val

Thanks for your help.

Community
  • 1
  • 1
Markus
  • 5,667
  • 4
  • 48
  • 64

0 Answers0