我想知道如果我正在以正确的方式进行操作。打开文件后检查文件是否被修改关闭
def checkout
clone = system("svn export #{file} tmp/")
open_file = system("start tmp/#{@file}")
end
现在,我可以使用默认编辑器打开我想要的文件,但是如何在关闭之前记录文件是否被修改。
我应该创建一个Process
并做Process.wait
什么?
感谢您的帮助
我想知道如果我正在以正确的方式进行操作。打开文件后检查文件是否被修改关闭
def checkout
clone = system("svn export #{file} tmp/")
open_file = system("start tmp/#{@file}")
end
现在,我可以使用默认编辑器打开我想要的文件,但是如何在关闭之前记录文件是否被修改。
我应该创建一个Process
并做Process.wait
什么?
感谢您的帮助
如果你的意思是你是在Windows中使用start
,使用/wait
或/w
选项,使其等到编辑终止。
使用IO::read
检查文件内容修改。 (在编辑器执行之前,之后)。
before = IO.read('tmp/#{@file}', {mode: 'rb'})
system("start /wait tmp/#{@file}")
after = IO.read('tmp/#{@file}', {mode: 'rb'})
# Check the file content modification.
if before != after:
# File changed!
如果你正在编辑一个巨大的文件,IO::read
会相应地消耗memroy。如Arup Rakshit建议使用File::mtime
,如果存储库中存在如此庞大的文件。 (缺点:假阳性警报保存未经修改)
用于同一File::mtime
方法。
将指定文件的修改时间作为Time对象返回。
file_time_before_opening = File.mtime('your file/path')
# do file operation as you like
file_time_after_closing = File.mtime('your file/path')
# now compare file_time_before_opening and file_time_after_closing to know
# if it is modified or not.
听起来很有趣去检查出 – Supersonic
用户,打开文件和before_opening时间已设置,用户尚未完成编辑该文件,我已经获得相同的时间before_closing – Supersonic
如果它的一个巨大的文件,你会消耗太多的内存这个微不足道的任务,检查修改时间似乎是这样做的最适当的方法 – bjhaid
+1 /等,如果我知道我不会问这个问题谢谢 – Supersonic
@bjhaid,我提到了内存消耗。感谢您的反馈。 – falsetru