如何比较Yaml文件中的键? [英] How to compare keys in yaml files?

查看:111
本文介绍了如何比较Yaml文件中的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆