Class: Hash

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

Overview

Extension methods for hash class

Instance Method Summary collapse

Instance Method Details

#deep_key_pathsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/extentions/hash.rb', line 12

def deep_key_paths
  stack = map { |k, v| [[k], v] }
  key_map = []

  until stack.empty?
    key, value = stack.pop

    key_map << key unless value.is_a? Hash

    next unless value.is_a? Hash

    value.map do |k, v|
      stack.push [key.dup << k, v]
    end
  end

  key_map.reverse
end

#deep_keysObject



5
6
7
8
9
10
# File 'lib/extentions/hash.rb', line 5

def deep_keys
  each_with_object([]) do |(k, v), keys|
    keys << k
    keys << v.deep_keys if v.respond_to?(:keys)
  end
end

#sanitize!(keys) ⇒ Object



31
32
33
34
35
# File 'lib/extentions/hash.rb', line 31

def sanitize!(keys)
  keep_if do |k, _v|
    keys.include?(k)
  end
end