Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/nested_hash_helper.rb
Instance Method Summary collapse
- #_find_depth(maxx_depth, current_depth) ⇒ Object
- #deep_delete(key) ⇒ Object
- #deep_delete_empty ⇒ Object
-
#deep_except(*excluded_keys) ⇒ Object
Your code goes here…
- #find_all_values(key) ⇒ Object
- #find_deep_intersection(compare_hash) ⇒ Object
- #find_deep_keys(value) ⇒ Object
- #find_depth ⇒ Object
- #hash_to_array ⇒ Object
Instance Method Details
#_find_depth(maxx_depth, current_depth) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/nested_hash_helper.rb', line 37 def _find_depth(maxx_depth , current_depth) current_class = self.class self.each do |current_keys , current_value | if current_value.is_a?(current_class) maxx_depth = current_value._find_depth(maxx_depth , current_depth + 1) end end maxx_depth = (maxx_depth > current_depth)? maxx_depth : current_depth end |
#deep_delete(key) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/nested_hash_helper.rb', line 111 def deep_delete(key) current_class = self.class self.each do |current_keys , current_value| if current_keys == key self.delete(current_keys) elsif current_value.is_a?(current_class) current_value.deep_delete(key) end end end |
#deep_delete_empty ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nested_hash_helper.rb', line 18 def deep_delete_empty current_class = self.class self.each do |current_keys , current_value| if current_value.nil? || current_value.empty? self.delete(current_keys) next end if current_value.is_a?(current_class) current_value.deep_delete_empty end end end |
#deep_except(*excluded_keys) ⇒ Object
Your code goes here…
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/nested_hash_helper.rb', line 5 def deep_except(*excluded_keys) current_class = self.class self.each do |current_keys , current_value| if excluded_keys.include?(current_keys) self.delete(current_keys) next end if current_value.is_a?(current_class) current_value.deep_except(*excluded_keys) end end end |
#find_all_values(key) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/nested_hash_helper.rb', line 98 def find_all_values(key) current_class = self.class values = [] self.each do |current_keys , current_value| if current_value.is_a?(current_class) values += current_value.find_all_values(key) elsif current_keys == key values.push(current_value) end end values end |
#find_deep_intersection(compare_hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/nested_hash_helper.rb', line 47 def find_deep_intersection(compare_hash) current_class = self.class final_hash = current_class.new self.each do |current_keys , current_value | if compare_hash.has_key?(current_keys) if current_value.is_a?(current_class) && compare_hash.fetch(current_keys).is_a?(current_class) final_hash[current_keys] = current_value.find_deep_intersection(compare_hash.fetch(current_keys)) elsif !current_value.is_a?(current_class) && !compare_hash.fetch(current_keys).is_a?(current_class) && (current_value == compare_hash.fetch(current_keys) ) final_hash[current_keys] = current_value end end end final_hash end |
#find_deep_keys(value) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nested_hash_helper.rb', line 62 def find_deep_keys(value) current_class = self.class deep_keys = [] self.each do |current_keys , current_value| if !current_value.is_a?(current_class) && current_value == value deep_keys = deep_keys.push(current_keys) elsif current_value.is_a?(current_class) future_deep_keys = current_value.find_deep_keys(value) if future_deep_keys.size >= 1 deep_keys.push(current_keys) deep_keys += future_deep_keys return deep_keys end end end deep_keys end |
#find_depth ⇒ Object
31 32 33 34 35 |
# File 'lib/nested_hash_helper.rb', line 31 def find_depth maxx_depth = 1 current_depth = 1 self._find_depth(maxx_depth , current_depth) end |
#hash_to_array ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/nested_hash_helper.rb', line 80 def hash_to_array current_class = self.class final_array = [] self.each do | current_keys , current_value | temp_array = [] if current_value.is_a?(current_class) temp_array.push(current_keys) temp_array += current_value.hash_to_array final_array.push(temp_array) else temp_array.push(current_keys) temp_array.push(current_value) final_array.push(temp_array) end end final_array end |