Created
July 11, 2016 13:40
-
-
Save arturictus/a34bf31dbca8a1da54c595223125246f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# merge_present | |
# examples: | |
# {hello: ''}.merge_present(hello: 'sd') #=> {hello: 'sd'} | |
# | |
# {hello: nil}.merge_present(hello: '') #=> {} | |
# | |
def merge_present(other_hash) | |
mapped = self.map do |k, v| | |
if v.blank? | |
if _new = other_hash[k.to_s].presence | |
[k, _new] | |
else | |
[k, nil] | |
end | |
else | |
[k, v] | |
end | |
end | |
Hash[mapped].compact | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment