本文介绍了同步两个 YAML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有插件可以同步两个 YAML 文件?例如 file1 有
Is there any plugin to synchronize two YAML files ? For example file1 has
en:
key1: "Value1"
key2: "Value2"
es:
key1: "EsValue1"
同步后需要在第二个文件中添加key2:Value2",不影响顺序,第二个文件中添加key1.
After synchronizing it needs to add key2: "Value2" in the second file without disturbing the order and the key1 in the second file.
推荐答案
你真的不需要插件来做到这一点:
You don't really need a plugin to do it:
str = <<EOT
en:
key1: "Value1"
key2: "Value2"
es:
key1: "EsValue1"
EOT
require 'yaml'
yaml = YAML::load(str)
(hash['en'].keys - hash['es'].keys).each{ |k| hash['es'][k] = hash['en'][k] }
>> ap hash #=> nil
{
"en" => {
"key1" => "Value1",
"key2" => "Value2"
},
"es" => {
"key1" => "EsValue1",
"key2" => "Value2"
}
}
如果您要处理任意数量的其他哈希:
If you have an arbitrary number of other hashes to process:
(yaml.keys - ['en']).each do |h|
(yaml['en'].keys - yaml[h].keys).each do |k|
yaml[h][k] = yaml['en'][k]
end
end
因此,读取 YAML 文件,通过代码运行生成的哈希,然后再次写入文件.
So, read the YAML file, run the resulting hash through the code, then write the file again.
这篇关于同步两个 YAML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!