Class: Hash

Inherits:
Object show all
Includes:
JamfRubyExtensions::Hash::Utils
Defined in:
lib/jamf/ruby_extensions/hash.rb

Overview

include the modules loaded above

Instance Method Summary collapse

Instance Method Details

#j_nillify!(to_nils = '', recurse = false) {|value| ... } ⇒ Hash Also known as: jss_nillify! Originally defined in module JamfRubyExtensions::Hash::Utils

Convert Hash values to nil. This is useful in the Classic API due to lack of consistency as to whether unset values come to us as nils or empty strings. This APIObject class converts all empty strings to nils using this method.

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.

Examples:

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 }}

Parameters:

  • to_nils (String, Array) (defaults to: '')

    Hash values equal to (==) these become nil. Defaults to empty string

  • recurse (Boolean) (defaults to: false)

    should sub-Hashes be nillified?

Yields:

  • (value)

    Hash values for which the block returns true will become nil.

Returns:

  • (Hash)

    the hash with the desired values converted to nil