Supersonic
Supersonic

Reputation: 430

after opening a file check if the file was modified on close

I am wondering if I am doing it the correct way.

def checkout
  clone = system( "svn export #{file} tmp/" )
  open_file = system( "start tmp/#{@file}" )
end

Now, I am able to open the file I want with the default editor but how to record if the file was modified before close.

Should I create a Process and do Process.wait or something?

Thanks for your help

Upvotes: 1

Views: 186

Answers (2)

falsetru
falsetru

Reputation: 369124

If you mean you are using start in Windows, use /wait or /w option to make it wait until the editor termination.

Use IO::read to check file content modification. (before, after the editor execution).

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!

If you're editing a huge file, IO::read will consume memroy accordingly. Use File::mtime as Arup Rakshit suggested, if there's such huge file in your repository. (cons: false positive alarm for save without modification)

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118271

Use File::mtime method for the same.

Returns the modification time for the named file as a Time object.

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.

Upvotes: 2

Related Questions