简体   繁体   中英

Parse existing YAML file to create new template (remove value from key-value pair)

I'm looking to take an existing YAML file, configured like this:

en: 
  calendars: 
    gregorian: 
      days: 
        format: 
          abbreviated: 
            fri: Fri
            mon: Mon
            sat: Sat
            sun: Sun
            thu: Thu
            tue: Tue
            wed: Wed
          narrow: :"calendars.gregorian.days.stand-alone.narrow"
          wide: 
            fri: Friday
            mon: Monday
            sat: Saturday
            sun: Sunday
            thu: Thursday
            tue: Tuesday
            wed: Wednesday
(etc.)

And remove the values from the key-value pair. Basically it would look like:

en: 
  calendars: 
    gregorian: 
      days: 
        format: 
          abbreviated: 
            fri:
            mon:
            sat:
            sun:
            thu:
            tue:
            wed:

I used Psych in 1.9.3 to import the YAML file as a hash ( Psych.load_file etc.) - is there a function I can call via Psych or maybe just plain Ruby to remove all the values from these pairs?

If the yaml has been loaded in a hash, just use ruby to transform the data structure.

format = loaded_yaml['en']['calendars']['gregorian']['days']['format']
format['abbreviated'] = format['abbreviated'].keys.inject({}) {|m,k| m[k] = nil; m }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM