ruby-inotify example?

梦想与她 提交于 2019-12-08 07:17:32

问题


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.



来源:https://stackoverflow.com/questions/7424212/ruby-inotify-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!