Skip to content

Instantly share code, notes, and snippets.

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 davelyon/359160 to your computer and use it in GitHub Desktop.
Save davelyon/359160 to your computer and use it in GitHub Desktop.
# recursively compare the keys of these hashes, making sure that two has the same keys as one
one = {:id => 1, :items => [{:name => 'one'}, {:name => 'one:one'}]}
two = {:id => 2, :items => [{:name => 'two'}, {:name => 'two:two'}]}
def check_keys(hash1, hash2)
hash1.each_key do |key|
if hash1[key].is_a? Array
return hash1[key].each_index {|sub| check_keys(hash1[key][sub], hash2[key][sub])} if hash1[key].is_a? Hash
elsif hash1[key].is_a? Hash
return check_keys(hash1[key], hash2[key])
else
return hash1[key].is_a? hash2[key].class
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment