Module: CoreExtensions::Hash::Pruning

Defined in:
lib/core_extensions/hash/pruning.rb

Overview

Monkey-patches for Hash to add some recursive pruning options

Instance Method Summary collapse

Instance Method Details

#pruneHash

Recursively strips empty and nil elements from a Hash

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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