Skip to content

Instantly share code, notes, and snippets.

@fj fj/compare.rb
Created May 5, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.