Skip to content

Instantly share code, notes, and snippets.

@radiospiel
Created August 29, 2011 10:57
Show Gist options
  • Save radiospiel/1178176 to your computer and use it in GitHub Desktop.
Save radiospiel/1178176 to your computer and use it in GitHub Desktop.
class File
class Fingerprint
attr_reader :inode
def initialize(path)
@path = path
@inode = File.stat(@path).ino rescue nil
end
def ==(other)
return false if self.inode.nil? || other.inode.nil?
self.inode == other.inode || self.md5 == other.md5
end
def md5
@md5 ||= Digest::MD5.hexdigest File.read(@path)
end
def inspect
"<#{@path}: #{inode}/#{@md5 || 'outstanding md5...'}"
end
end
def self.fingerprint(path)
Fingerprint.new path
end
end
module Enumerable
def unique?(&block)
first_loop = true
each do |entry|
if first_loop
value = yield(entry)
first_loop = false
else
return false if value != yield(entry)
end
end
true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment