Class: FindDuplicates::Groups

Inherits:
Object
  • Object
show all
Defined in:
lib/find-duplicates.rb

Instance Method Summary collapse

Constructor Details

#initializeGroups

Returns a new instance of Groups.



30
31
32
# File 'lib/find-duplicates.rb', line 30

def initialize
  @hashes = {}    
end

Instance Method Details

#add(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/find-duplicates.rb', line 33

def add(path)
  md5 = Digest::MD5.new
  begin
    File.open(path, 'rb') do |file_h|
      file_h.each(8192) do |block|
        md5.update block
      end
    end
  rescue
    $stderr.puts "problem: #{ $! }"
    return
  end
  digest = md5.digest
  if @hashes.key?(digest)
    @hashes[digest] << path
  else
    @hashes[digest] = [ path ]
  end
end

#duplicatesObject



52
53
54
# File 'lib/find-duplicates.rb', line 52

def duplicates
  @hashes.find_all { |k,v| v.size > 1 }    
end