Skip to content

Instantly share code, notes, and snippets.

@arturictus
Created July 11, 2016 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturictus/a34bf31dbca8a1da54c595223125246f to your computer and use it in GitHub Desktop.
Save arturictus/a34bf31dbca8a1da54c595223125246f to your computer and use it in GitHub Desktop.
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