lib/jss/ruby_extensions/hash.rb
#
# Convert Hash values to nil.
#
# With no block, values equalling the String, or any member of the Array, given
# will be converted to nil. Equality is evaluated with == and Array#include?
#
# With a block, if the result of the block evaluates to true, the value is converted to nil.
#
# Subhashes are ignored unless recurse is true.
#
# @param to_nils[String,Array] Hash values equal to (==) these become nil. Defaults to empty string
#
# @param recurse[Boolean] should sub-Hashes be nillified?
#
# @yield [value] Hash values for which the block returns true will become nil.
#
# @return [Hash] the hash with the desired values converted to nil
#
# @example
#   hash = {:foo => '', :bar => {:baz => '' }}
#   hash.jss_nillify!  # {:foo => nil, :bar => {:baz => '' }}
#
#   hash = {:foo => '', :bar => {:baz => '' }}
#   hash.jss_nillify! '', :recurse  # {:foo => nil, :bar => {:baz => nil }}
#
#   hash = {:foo => 123, :bar => {:baz => '', :bim => "123" }}
#   hash.jss_nillify! ['', 123], :recurse # {:foo => nil, :bar => {:baz => nil, :bim => "123" }}
#
#   hash = {:foo => 123, :bar => {:baz => '', :bim => "123" }}
#   hash.jss_nillify!(:anything, :recurse){|v| v.to_i == 123 }  # {:foo => nil, :bar => {:baz => '', :bim => nil }}
#
Convert Hash values to nil.
With no block, values equalling the String, or any member of the Array, given
will be converted to nil.

With a block, if the result of the block evaluates to true, the value is converted to nil.

Subhashes are ignored unless recurse is true.
Convert Hash values to nil . With no block values equalling the String or any member of the Array given will be converted to nil . With a block if the result of the block evaluates to true the value is converted to nil . Subhashes are ignored unless recurse is true .
def jss_nillify!(to_nils = '', recurse = false, &block)
    nillify_these = [] << to_nils
    nillify_these.flatten!
    each_pair do |k, v|
      if v.class == Hash
        v.jss_nillify!(to_nils, recurse, &block)
        next
      end
      do_it =
        if block_given?
          yield v
        else
          nillify_these.include? v
        end
      self[k] = nil if do_it
    end 
  end
def jss_nillify! ( to_nils = '' , recurse = false , & block ) nillify_these = [ ] << to_nils nillify_these . flatten! each_pair do | k , v | if v . class == Hash v . jss_nillify! ( to_nils , recurse , & block ) next end do_it = if block_given? yield v else nillify_these . include? v end self [ k ] = nil if do_it end end
Convert Hash values to nil.
Convert Hash values to nil .
# each pair
the hash with the desired values converted to nil
the hash with the desired values converted to nil
Hash
to_nils
Hash values equal to (==) these become nil. Defaults to empty string
Hash values equal to ( == ) these become nil . Defaults to empty string
recurse
None
block
]
Hash values for which the block returns true will become nil.
Hash values for which the block returns true will become nil .
example
None