From: andrew_queisser@... (andrew queisser) Date: 2002-03-27T02:27:32+09:00 Subject: Re: File change notification? Dale Martenson wrote in message news:<20C86D2620F6D411A199009005DC0102F52A96@exchange_serve.multitech.prv>... > >> Is there a straightforward way of getting notified when a file > >> changes? In NT I can block a thread on the handle returned by > >> FindFirstChangeNotification. Does something like this exist in Ruby? > >> I'm currently using the Cygwin version but could be convinced to > >> switch. > > Here is a little hack that might work for you. There may be a better way, > but I am still learning Ruby myself. > > class FileCheck < Thread > def initialize( filename, callback, checktime=2 ) > currentModTime = File.stat(filename).mtime > return super { > while true > if currentModTime < File.stat(filename).mtime then > callback.call > currentModTime = File.stat(filename).mtime > end > sleep(checktime) > end > } > end > end > > # Do this when the file changes > def fcAction > print "#{Time.now}: Something changed!!\n" > end > > # Create a file check on the passed in argument > fc = FileCheck.new( ARGV.shift, proc { fcAction } ) > fc.run > > # do something useful for a while (hopefully not this) > while true > print "#{Time.now}: Nothing yet.\n" > sleep(3) > end Thanks, I was considering something like that myself buy hadn't written anything yet. I'd like to stay away from polling, though. I don't know enough about UNIX-style system calls to know whether there's a built-in way of getting notified a la NT. If there is, I suppose that it would be easy to support the same to the major Ruby platforms. Andrew