How to monitor individual files with Ruby listen gem?
filesystems, guard, listen, ruby
Solution
I wasn't paying attention to what I was doing here. `only:` clearly takes a regex, which I was obviously failing to construct and provide. Hopefully this can help someone else out who may need to use `listen` to watch or ignore specific files.
Here is an example of my working solution:
module FileTracker
filedir = "/home/user/dir"
filenames = [ "file1", "file2" ]
listenRegex = filenames.join("$|").gsub!(".", "\.") + "$"
# Should only track 'file1' and 'file2' in this directory
listener = Listen.to(filedir, only: /#{listenRegex}/) do |modified, added, removed|
puts "Updated: " + modified.first
end
listener.start
sleep
end
Problem
I'm trying to use the listen gem (https://github.com/guard/listen) to listen to specific files, but so far have only been able to get the base functionality of listening to whole directories working properly. Here's an example of what I'm trying at the moment: ``` require "listen" module FileTracker filedir = "/home/user/dir" filenames = [ "file1", "file2" ] # Should only track 'file1' and 'file2' in this directory listener = Listen.to(filedir, only: filenames) do |modified, added, removed| puts "Updated: " + modified.first end listener.start sleep end ``` I have also tried simply specifying a single filename as a string for the `only:` parameter, but that didn't work either. My end goal is to be able to track a list of user-defined files in various directories for changes. I only want to track each specific file, not the the entire directory.