Skip to content

Instantly share code, notes, and snippets.

@hide24
Created November 21, 2013 08:15
Show Gist options
  • Save hide24/7577787 to your computer and use it in GitHub Desktop.
Save hide24/7577787 to your computer and use it in GitHub Desktop.
recursive different search from two version of transration_??.yml.
class Hash
def rdiff(other)
new_hash = {}
self.each do |key, value|
if other.key?(key)
if value.kind_of?(Hash)
if other[key].kind_of?(Hash)
diff = value.rdiff(other[key])
unless diff.empty?
new_hash[key] = diff
end
else
new_hash[key] = value
end
else
unless value == other[key]
new_hash[key] = value
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.rdiff(old_yaml)
puts YAML.dump(appended_yaml)
# ---
# en:
# activerecord:
# metadata:
# entity_id: entityID
# 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