File: hash_extensions.rb

package info (click to toggle)
ruby-table-print 1.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 296 kB
  • sloc: ruby: 1,823; makefile: 2
file content (37 lines) | stat: -rw-r--r-- 830 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module TablePrint
  module HashExtensions
    module ConstructiveMerge
      def constructive_merge(hash)
        target = dup

        hash.keys.each do |key|
          if hash[key].is_a? Hash and self[key].is_a? Hash
            target[key].extend ConstructiveMerge
            target[key] = target[key].constructive_merge(hash[key])
            next
          end

          target[key] = hash[key]
        end

        target
      end

      def constructive_merge!(hash)
        target = self

        hash.keys.each do |key|
          if hash[key].is_a? Hash and self[key].is_a? Hash
            target[key].extend ConstructiveMerge
            target[key] = target[key].constructive_merge(hash[key])
            next
          end

          target[key] = hash[key]
        end

        target
      end
    end
  end
end