Created
September 29, 2011 17:24
-
-
Save longlostnick/1251339 to your computer and use it in GitHub Desktop.
Delete all empty/false elements from hash recursively
This file contains 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
# (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
implementation that works for both hashes and arrays