在rails国际化yaml文件上有两个ruby。一个文件已完成,而另一个文件缺少 key 。如何比较两个Yaml文件并查看第二个文件中缺少的 key ?有什么工具可以做到这一点吗?
最佳答案
假设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
现在
compare_yaml_hash(YAML.load_file("file1"), YAML.load_file("file2"))
限制:如果您的YAML文件包含数组,则当前实现应扩展为支持数组。
关于ruby-on-rails - 如何比较Yaml文件中的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274126/