Skip to content

Instantly share code, notes, and snippets.

@acuppy
Created February 6, 2014 17:07
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 acuppy/8848391 to your computer and use it in GitHub Desktop.
Save acuppy/8848391 to your computer and use it in GitHub Desktop.
Recursively delete hash key/value pairs where the value is nil
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