What is the best way in ruby to check if a file was actually modified?
I'm writing an application that convert data serialized in ruby (file A) to xml format (file B). To avoid reconverting unchanged files i already added a modification date check: if the last modification in file A is older 开发者_如何学Pythonthan the last modification in file B we can avoid a new conversion.
Unf. some file A is overwritten but mantains the same content. This makes the conversion process futile and i'd like to avoid it.
I was thinking of storing on the disk an hash of the last converted file A and then before converting i could check if the hash of the file had changed.
Is there a way to easily create such a hash code in ruby?
require 'digest/sha1'
Digest::SHA1.hexdigest(File.read("/a")) # => "da39a3ee5e6b4b0d3255bfef95601890afd80709"
How big are your files?
If they're not that big, you can write a copy of file A to a file C every time file B is updated.
When you want to update file B again, you perform a diff on files A and B, if they are different, you update file B.
There are other solutions:
rewrite the serialized file and insert a comment at the top or just append a comment to the end that marks it as already-converted
move the files to a subdirectory after conversion
精彩评论