Created
February 6, 2014 17:07
-
-
Save acuppy/8848391 to your computer and use it in GitHub Desktop.
Recursively delete hash key/value pairs where the value is nil
This file contains hidden or 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
class Hash | |
def compact(opts={}) | |
inject({}) do |new_hash, (k,v)| | |
if !v.nil? | |
new_hash[k] = opts[:recurse] && v.class == Hash ? v.compact(opts) : v | |
end | |
new_hash | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment