如何比较Yaml文件中的键? [英] How to compare keys in yaml files?
问题描述
在Rails国际化yaml文件中有两个ruby。一个文件完整,而另一个文件缺少密钥。如何比较两个Yaml文件并查看第二个文件中缺少的密钥?
There are two ruby on rails internationalization yaml files. One file is complete and another one is with missing keys. How can I compare two yaml files and see missing keys in second file? Are there any tools for doing that?
推荐答案
假设 file1
是解决方案
正确的版本,而 file2
是缺少键的版本:
def compare_yaml_hash(cf1, cf2, context = [])
cf1.each do |key, value|
unless cf2.key?(key)
puts "Missing key : #{key} in path #{context.join(".")}"
next
end
value2 = cf2[key]
if (value.class != value2.class)
puts "Key value type mismatch : #{key} in path #{context.join(".")}"
next
end
if value.is_a?(Hash)
compare_yaml_hash(value, value2, (context + [key]))
next
end
if (value != value2)
puts "Key value mismatch : #{key} in path #{context.join(".")}"
end
end
end
Now
现在
Limitation: Current implementation should be extended to support arrays if your YAML file contains arrays.
这篇关于如何比较Yaml文件中的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!