gpt4 book ai didi

ruby - 如何使用 ruby​​ 检测文件系统事件

转载 作者:数据小太阳 更新时间:2023-10-29 07:49:53 26 4
gpt4 key购买 nike

我想使用 ruby​​ 监视文件和目录的文件系统事件。

现在我正在使用 Guard。但它只检测文件事件。

有什么方法可以检测包括目录事件在内的文件系统事件吗?

最佳答案

您可以 rb-inotify用于监视特定目录的文件系统事件例如:

require "rb-inotify"

DIR_TO_WATCH = "/tmp/notify-dir"

notifier = INotify::Notifier.new

notifier.watch(DIR_TO_WATCH, :create, :delete) do |event|
puts "Create event for: #{event.name}" if event.flags.include?(:create)
puts "Delete event for: #{event.name}" if event.flags.include?(:delete)
end

notifier.run

希望对您有所帮助。

编辑

对于支持多个平台,你可以尝试Listen ,这是由 Guard 的人制作的,支持 Mac OS X 10.6+、Linux、BSD( some problems are there) 和 Windows。

来自文档:

listener = Listen.to('dir/to/listen', 'dir/to/listen2') do |modified, added, removed|
puts "modified absolute path: #{modified}"
puts "added absolute path: #{added}"
puts "removed absolute path: #{removed}"
end
listener.start # not blocking
sleep

关于ruby - 如何使用 ruby​​ 检测文件系统事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24487502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com