Module: Moleculer::Support::HashUtil

Extended by:
HashUtil
Included in:
HashUtil
Defined in:
lib/moleculer/support/hash_util.rb

Overview

A module of functional methods for working with hashes

Instance Method Summary collapse

Instance Method Details

#fetch(hash, key, default = :__no_default__) ⇒ Object

Works like fetch, but instead indifferently uses strings and symbols. It will try both snake case and camel case versions of the key.

is passed the hash will use an indifferent fetch

Parameters:

  • hash (Hash)

    the hash to fetch from

  • key (Object)

    the key to fetch from the hash. This uses Hash#fetch internally, and if a string or synbol

  • default (Object) (defaults to: :__no_default__)

    the a fallback default if fetch fails. If not provided an exception will be raised if key cannot be found

Returns:

  • (Object)

    the value at the given key



20
21
22
23
24
25
# File 'lib/moleculer/support/hash_util.rb', line 20

def fetch(hash, key, default = :__no_default__)
  return fetch_with_string(hash, key, default) if key.is_a?(String) || key.is_a?(Symbol)
  return hash.fetch(key, default) if default != :__no_default__

  hash.fetch(key)
end