Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a file change listener in Ruby

I want to write a listener (ruby module) to identify a file creation inside a folder. My scenario is as follows

  • I have a folder called (files)
  • I have a Rails project which will create a file (demo.txt) inside the folder ('file')
  • I need to write a listener to identify the file change and start reading the file (demo.txt)

Where can I start on creating this Ruby module?

I'm using 'Ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]'.

like image 642
sameera207 Avatar asked Aug 05 '11 10:08

sameera207


1 Answers

There are a few small libraries, which you could utilize, learn from or build upon, e.g.

  • https://github.com/mynyml/watchr

Agile development tool that monitors a directory tree, and triggers a user defined action whenever an observed file is modified. Its most typical use is continuous testing, and as such it is a more flexible alternative to autotest.

  • http://codeforpeople.rubyforge.org/directory_watcher/

The directory watcher operates by scanning a directory at some interval and generating a list of files based on a user supplied glob pattern. As the file list changes from one interval to the next, events are generated and dispatched to registered observers. Three types of events are supported — added, modified, and removed.

  • https://github.com/guard/guard

Guard is a command line tool to easily handle events on files modifications (FSEvent / Inotify / Polling support).

  • http://rubydoc.info/gems/rb-inotify/0.8.6/frames

This is a simple wrapper over the inotify Linux kernel subsystem for monitoring changes to files and directories. It uses the FFI gem to avoid having to compile a C extension.

like image 145
miku Avatar answered Sep 19 '22 09:09

miku