Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge Nicely
class Hash
# Merges attributes nicely instead of just overwriting them.
#
# { class: 'large' }.merge_nicely!( class: 'label' ) #=> { class: 'large label' }
#
def merge_nicely!( hash_to_add = {} )
self.merge!(hash_to_add.to_hash) do |key, original_element, new_element|
if original_element.is_a? String and new_element.is_a? String
original_element + " " + new_element
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment