Skip to content

Instantly share code, notes, and snippets.

@wteuber
Forked from carlosveucv/yaml_cmp.rb
Created September 5, 2016 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wteuber/83e72ef93a3a88e1f1ada5ddec647953 to your computer and use it in GitHub Desktop.
Save wteuber/83e72ef93a3a88e1f1ada5ddec647953 to your computer and use it in GitHub Desktop.
Compare 2 yaml's in ruby (print matching keys)
require 'yaml'
def compare_yaml_hash(cf1, cf2, context = [])
cf1.each do |key, value|
if cf2.key?(key)
puts "Contains key : #{key} in path #{context.join(".")}"
end
value2 = cf2[key]
next unless value2
if value.is_a?(Hash)
compare_yaml_hash(value, value2, (context + [key]))
next
end
end
end
f1 = ARGV[0]
f2 = ARGV[1]
compare_yaml_hash(YAML.load_file(f1), YAML.load_file(f2))
# Based on code posted on http://stackoverflow.com/questions/6274126/how-to-compare-keys-in-yaml-files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment