Created
August 29, 2011 10:57
-
-
Save radiospiel/1178176 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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