Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cooljl31/e12e38f751eefb692069eb49988ac473 to your computer and use it in GitHub Desktop.
Save cooljl31/e12e38f751eefb692069eb49988ac473 to your computer and use it in GitHub Desktop.
Delete all empty/false elements from hash recursively
# (v.respond_to?(:empty?) ? v.empty? : !v) is basically rails' .blank? in plain ruby
class Hash
def delete_blank
delete_if do |k, v|
(v.respond_to?(:empty?) ? v.empty? : !v) or v.instance_of?(Hash) && v.delete_blank.empty?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment