Class: HashPruner

Inherits:
Object
  • Object
show all
Defined in:
lib/superdupe/hash_pruner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHashPruner

Returns a new instance of HashPruner.



8
9
10
# File 'lib/superdupe/hash_pruner.rb', line 8

def initialize()
  @visited = {}
end

Class Method Details

.prune(hash) ⇒ Object



3
4
5
# File 'lib/superdupe/hash_pruner.rb', line 3

def prune(hash)
  HashPruner.new.prune hash
end

Instance Method Details

#prune(item) ⇒ Object



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

def prune(item)
  if item.kind_of? Array
    item.map {|i| prune(i)}.reject {|v| v==nil}
  elsif item.kind_of? Hash
    if @visited[item.object_id]
      item.dup.delete_if {|k,v| v.kind_of?(Hash) || v.kind_of?(Array)}
    else
      @visited[item.object_id] = true
      new_hash = {}
      item.each do |k,v|
        new_hash[k] = prune(v)
      end
      @visited.delete item.object_id
      new_hash
    end
  else
    item
  end
end