ruby-inotify example?

2019-06-04 19:00发布

问题:

I'm looking for a simple, concise example of using the inotify gem to detect a change to a directory.

It lacks examples.

回答1:

There's an example in examples/watcher.rb. That link is to aredridel's repo, since it looks like you linked to aredridel's docs, and aredridel is the one who wrote the example.



回答2:

One of my projects I have used ruby-inotify for monitoring file creation under specific folder using following code

# frozen_string_literal: true

require 'rb-inotify'

# observe indicate folder, trigger event after
module ObserveFiles
  def self.observe
    watcher = INotify::Notifier.new
    directory = CONFIG['xml_folder'] # folder that want to watch
    watcher.watch(directory, :create) do |event|
      # do your work where 
      # here, event.name is created file name
      # event.absolute_name file absolute path
    end
    watcher.run
  end
end

Use this code like

ObserveFiles.observe

Hope this will help someone.



标签: ruby inotify