Skip to content

Instantly share code, notes, and snippets.

@joshuapinter
Last active August 29, 2015 14:06
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 joshuapinter/78a5545d713ab9d55883 to your computer and use it in GitHub Desktop.
Save joshuapinter/78a5545d713ab9d55883 to your computer and use it in GitHub Desktop.
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