Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/core_ext.rb
Overview
Here we add some methods better work with Tries
Instance Method Summary collapse
- #clone_values(from_keys: nil, to_keys: nil, suffix: nil, branch_a: nil, branch_b: nil) ⇒ Object
- #create_branch!(*keys) ⇒ Object
- #deep_dup ⇒ Object
Instance Method Details
#clone_values(from_keys: nil, to_keys: nil, suffix: nil, branch_a: nil, branch_b: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/core_ext.rb', line 36 def clone_values(from_keys: nil, to_keys: nil, suffix: nil, branch_a: nil, branch_b: nil) branch_a ||= dig(*from_keys) if branch_b.nil? create_branch!(*to_keys) branch_b = dig(*to_keys) end branch_a.each do |key, val| if val.is_a?(Hash) clone_values branch_a: branch_a[key], branch_b: (branch_b[key] ||= {}), suffix: suffix else branch_b[key] = "#{val.dup}#{suffix}" end end end |
#create_branch!(*keys) ⇒ Object
59 60 61 62 63 |
# File 'lib/core_ext.rb', line 59 def create_branch!(*keys) return nil if keys.empty? key = keys.shift (self[key] ||= {}).create_branch!(*keys) end |
#deep_dup ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/core_ext.rb', line 65 def deep_dup dup_hash = {} each do |k, v| dup_hash[k] = if v.is_a?(Hash) v.deep_dup else v.dup end end dup_hash end |