Skip to content

Instantly share code, notes, and snippets.

@fj
Created May 5, 2017 21:44
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 fj/d036cdb127bfb0fb1a24745db1bbacdf to your computer and use it in GitHub Desktop.
Save fj/d036cdb127bfb0fb1a24745db1bbacdf to your computer and use it in GitHub Desktop.
Compares two YAML files and shows which keys are "missing" and which keys are "extras"
require 'yaml'
class Hash
def fully_qualified_keys
map { |k,v|
v.fully_qualified_keys.map{ |a| "#{k}.#{a}" } rescue k.to_s
}.flatten
end
end
def yaml_hash_for(file)
YAML.load(File.open(File.expand_path file))
end
h1 = yaml_hash_for ARGV[0]
h2 = yaml_hash_for ARGV[1]
def compare(expected, actual)
expected_keys = expected.fully_qualified_keys
actual_keys = actual.fully_qualified_keys
missing = expected_keys - actual_keys
extraneous = actual_keys - expected_keys
if missing.any?
puts "Expected these keys, but didn't find them:"
missing.each { |key| puts " - #{key}" }
end
if extraneous.any?
puts "Found these keys, but didn't expect them:"
extraneous.each { |key| puts " + #{key}" }
end
end
compare(h1, h2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment