5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveSupport::FileUpdateChecker

Posted at

たまたま見つけたので使い方をメモ。

指定されたファイルが更新されたら、何かを実行するというのを
簡潔にかけるようになる。

サンプルコード
# coding: utf-8
require 'active_support/file_update_checker'

path = File.absolute_path('./foo.txt')
checker = ActiveSupport::FileUpdateChecker.new([path]) do
  puts File.read(path)
end  

10.times do |i|
  puts 'check: ' . i.to_s
  checker.execute_if_updated # 更新されていたら、blockを実行する
  # checker.execute # こっちは更新されていなくても実行する
  sleep(2)
end

このスクリプトを動かしながら、
以下を実行すると、puts File.read(path)の結果が表示される。

echo "foo" >> foo.txt

中々使い道が難しい。

see: https://github.com/rails/rails/blob/80256abb39332dd49996b909d6f0413a15291a90/activesupport/lib/active_support/file_update_checker.rb#L22

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?