module CoreExtensions::Hash::Pruning

Monkey-patches for Hash to add some recursive pruning options

Public Instance Methods

prune() click to toggle source

Recursively strips empty and nil elements from a Hash @return [Hash]

# File lib/core_extensions/hash/pruning.rb, line 9
def prune
  newhash = {}

  each do |k, v|
    if v.is_a?(Hash)
      newvalue = v.prune
      newhash[k] = newvalue unless newvalue.empty?
    elsif v.respond_to?(:empty?)
      newhash[k] = v unless v.empty?
    else
      newhash[k] = v unless v.nil?
    end
  end

  newhash
end