Module: HashDeepReject

Included in:
Hash
Defined in:
lib/hash_deep_reject.rb,
lib/hash_deep_reject/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#deep_reject!(call_on_hash = false, &blk) ⇒ Object

Takes nested Hashes: And rejects values that return true from the block:

{a: {b: {c: ""}, d: ""}}.deep_reject! { |k,v| v.blank? }
# => {:a=>{:b=>{}}}

If you want to call the block on Hash-type values after they’ve been iterated thru, pass true as the first argument.



11
12
13
14
15
16
17
18
19
20
# File 'lib/hash_deep_reject.rb', line 11

def deep_reject!(call_on_hash=false, &blk)
  delete_if do |k,v|
    if v.is_a?(Hash)
      v.deep_reject!(call_on_hash, &blk)
      call_on_hash ? blk.call(k,v) : false
    else
      blk.call(k,v)
    end
  end
end