Skip to content

Instantly share code, notes, and snippets.

View davelyon's full-sized avatar

Dave Lyon davelyon

View GitHub Profile
# 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])