lib/hm.rb
# Removes all "empty" values and subcollections (`nil`s, empty strings, hashes and arrays),
# including nested structures. Empty subcollections are removed recoursively.
#
# @example
#   order = {items: [{title: "Beef", price: 18.2}, {title: '', price: nil}], total: 26.2}
#   Hm(order).cleanup.to_h
#   # => {:items=>[{:title=>"Beef", :price=>18.2}], :total=>26.2}
#
# @return [self]
Removes all "empty" values and subcollections (`nil`s, empty strings, hashes and arrays),
including nested structures. Empty subcollections are removed recoursively.
Removes all " empty " values and subcollections ( ` nil ` s empty strings hashes and arrays ) including nested structures . Empty subcollections are removed recoursively .
def cleanup
    deletions = -1
    until deletions.zero?
      deletions = 0
      Algo.visit_all(@hash) do |at, path, val|
        if val.nil? || val.respond_to?(:empty?) && val.empty?
          deletions += 1
          Algo.delete(at, path.last)
        end
      end
    end
    self
  end
def cleanup deletions = - 1 until deletions . zero? deletions = 0 Algo . visit_all ( @hash ) do | at , path , val | if val . nil? || val . respond_to? ( :empty? ) && val . empty? deletions += 1 Algo . delete ( at , path . last ) end end end self end
Removes all "empty" values and subcollections (`nil`s, empty strings, hashes and arrays),
including nested structures.
Removes all " empty " values and subcollections ( ` nil ` s empty strings hashes and arrays ) including nested structures .
# We do several runs to delete recursively: {a: {b: [nil]}} # first: {a: {b: []}} # second: {a: {}} # third: {} # More effective would be some "inside out" visiting, probably
None
self
example
None