Radek
Radek

Reputation: 11121

how to reload external file every time?

I have a ruby script where my "config" is in an extra file. It's called ftp_config.rb. Then I have filetrack.rb that downloads files from a ftp server - what files/directories is specified in ftp_config.rb. And finally I got rufus_download.rb that is calling a function from filetrack.rb every day so I get all new files from the server.

Everything works fine just I want to know how to make it so when I edit ftp_config.rb the changes are picked up by the script without the need to restart rufus_download.rb.

currenly

Right now if I add new files to be downloaded to ftp_config.rb I need to restart rufus

Upvotes: 2

Views: 229

Answers (2)

Dan Myasnikov
Dan Myasnikov

Reputation: 158

require_relative returns false if the file you have requested is already loaded to your ruby script and returns true if you haven't

If you want changes to be loaded directly you need to load files

load 'path/to/ftp_config'

every time your script executes it will load / reload the script

EDIT:

you can load by expanding path of the current ruby script:

load ::File.expand_path('../ftp_config.rb', __FILE__)

Assuming that files are in the same folder

EDITEND

hope that helps

Upvotes: 2

peter
peter

Reputation: 42207

You need a gem that monitors filechanges like "sinatra/reloader" for Sinatra and eg filewatcher or listen for desktop apps. After detecting an update you load the script, not require, that only loads a script once.

Here an example of filewatcher.

STDOUT.sync = true

FileWatcher.new(['c:/test/scans']).watch() do |filename, event|
    puts filename
  if(event == :changed)
    puts "File updated: " + filename
  end
  if(event == :delete)
    puts "File deleted: " + filename
  end
  if(event == :new)
    puts "Added file: " + filename
  end
end 

Upvotes: 0

Related Questions