How can I sort YAML files?

前端 未结 9 958
灰色年华
灰色年华 2021-02-02 12:55

I\'ve been trying to sort an i18n translations YAML file with Ruby so I can manage new translations in a better and organized way, but I\'ve been wondering if there is something

9条回答
  •  遇见更好的自我
    2021-02-02 13:04

    Unfortunately YAML::quick_emit has been deprecated and is no longer available in newer builds of the Psych gem. If you want your hash keys to be sorted when serialized to yaml, you'll have to use the following monkey patch instead:

    class Hash
        def to_yaml opts={}
            return Psych.dump(self.clone.sort.to_h)
        end
    end
    

提交回复
热议问题