Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/core_ext.rb
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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/core_ext.rb', line 50 def clone_values(from_keys: nil, to_keys: nil, suffix: nil, branch_a: nil, branch_b: nil) branch_a ||= self.dig(*from_keys) if branch_b.nil? self.create_branch!(*to_keys) branch_b = self.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
68 69 70 71 72 |
# File 'lib/core_ext.rb', line 68 def create_branch!(*keys) return nil if keys.empty? key = keys.shift (self[key] ||= {}).create_branch!(*keys) end |
#deep_dup ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/core_ext.rb', line 74 def deep_dup dup_hash = {} self.each do |k,v| if v.is_a?(Hash) dup_hash[k] = v.deep_dup else dup_hash[k] = v.dup end end dup_hash end |