Skip to content

Instantly share code, notes, and snippets.

@hide24
Created November 21, 2013 06:25
Show Gist options
  • Save hide24/7576898 to your computer and use it in GitHub Desktop.
Save hide24/7576898 to your computer and use it in GitHub Desktop.
recursive delete of Hash.
class Hash
def rdelete(other)
new_hash = {}
self.each do |key, value|
if other.key?(key)
if value.kind_of?(Hash)
if other[key].kind_of?(Hash)
new_hash[key] = value.rdelete(other[key])
if new_hash[key].empty?
new_hash.delete(key)
end
else
new_hash[key] = value
end
else
if other[key].kind_of?(Hash)
new_hash[key] = value
else
# delete
end
end
else
new_hash[key] = value
end
end
new_hash
end
end
require 'yaml'
current_str = <<-EOS
en:
activerecord:
models:
metadata: "Metadata"
attributes:
metadata:
application_base_id: "ID Based on Application Form"
entity_id: "entityID"
certification: "Certificate"
status: "Status"
foo: "bar"
EOS
old_str = <<-EOS
en:
activerecord:
models:
metadata: "Metadata"
attributes:
metadata:
entity_id: "entityID"
certification: "Certificate"
status: "Status"
EOS
current_yaml = YAML.load(current_str)
old_yaml = YAML.load(old_str)
appended_yaml = current_yaml.rdelete(old_yaml)
puts YAML.dump(appended_yaml)
# ---
# en:
# activerecord:
# metadata:
# application_base_id: ID Based on Application Form
# foo: bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment