Class: HashWithIndifferentAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_with_indifferent_access.rb

Instance Method Summary collapse

Instance Method Details

#deep_find(path, default = nil) ⇒ Object

Perform a depth first search of this hash and return the first element matching path, or default if nothing found.

Parameters:

  • path (Array[String | Symbol] | String | Symbol)

    to the element of interest.

  • default (Object) (defaults to: nil)

    the object to be returned if there is no result for path.

Returns:

  • (Object)

    the first object found on path or default.



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

def deep_find(path, default = nil)
  return default unless path
  path = [path] if path.is_a?(String) || path.is_a?(Symbol)
  return default unless path.is_a?(Array) && !path.empty?

  location = self
  path.each do |key|
    return default if location.nil? || !location.key?(key)
    location = location[key]
  end
  return location
end