Created
April 19, 2014 15:10
-
-
Save wuhuizuo/11087142 to your computer and use it in GitHub Desktop.
Hash的深层merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def deep_merge!(other_hash) | |
other_hash.each_pair do |k, v| | |
tv = self[k] | |
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v | |
end | |
self | |
end | |
def deep_merge(other_hash) | |
dup.deep_merge!(other_hash) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment