Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/asana_exception_notifier/initializers/hash.rb

Overview

override Hash class

Instance Method Summary collapse

Instance Method Details

#deep_hash_with_parent(value, key, &block) ⇒ void

This method returns an undefined value.

Checks if the value is a Hash , and will execute the each with parent for the given hash

Parameters:

  • value (Hash)

    The Hash that will be used for iteration

  • key (Hash)

    The key that will be sent as the parent key of the specified Hash

  • &block (Proc)

    The block which will be used to yield the parent string, the current key and the value, while the Hash is being iterated over

See Also:



47
48
49
50
# File 'lib/asana_exception_notifier/initializers/hash.rb', line 47

def deep_hash_with_parent(value, key, &block)
  return unless value.is_a?(Hash)
  value.each_with_parent(key, &block)
end

#each_with_parent(parent = nil, &block) ⇒ void

This method returns an undefined value.

This method is used for iterating over a Hash , but besides yielding the key and the value, this will also yield the parent key of the current Hash object if the Hash is associated to a key

Examples:

Printing a Hash that contains other hashes inside, and fetching the parent keys

hash = {
         key: :my_value,
         key2: {
           innner_key: :inner_value
         }
       }
 hash.each_with_parent { |parent, key, value| puts [parent, key, value].inspect }
 will print:
     [nil, :key, :my_value]
     [:key2, :innner_key, :inner_value]

Parameters:

  • parent (String, nil) (defaults to: nil)

    The parent key of the current level of the Hash ( first level of any Hash has no parent, but if the hash has a value which is also a Hash, the parent of that value will be the key associated to the value )

  • &block (Proc)

    The block which will be used to yield the parent string, the current key and the value, while the Hash is being iterated over

See Also:



28
29
30
31
32
33
34
35
36
# File 'lib/asana_exception_notifier/initializers/hash.rb', line 28

def each_with_parent(parent = nil, &block)
  each do |key, value|
    if value.is_a?(Hash)
      deep_hash_with_parent(value, key, &block)
    elsif block_given?
      yield(parent, key, value)
    end
  end
end