Ruby application that keeps reloading source files

Asked

Viewed 126 times

5

How can I design my application so that whenever I modify and save a file . Rb the application will behave "the new way"?

I am making an application that reads the command, executes, prints the result and reads the next command (REPL, Read-Eval-Print-Loop). And I would like the sources to be reloaded whenever I modify them.

Kind of the way Rails and Unity(the graphical engine) do it when I change scripts.

Has anyone ever done something like this? How can I do something like this?

2 answers

9

You can use Gem Rerun. Install with:

gem install rerun

And if your show’s called script.rb, execute with:

rerun script.rb

It will monitor the file system and re-export the command whenever a file is changed. The only precaution to take into account is that it kills the previous process, so you may end up corrupting some configuration file (good programs should be resistant against this, it is a good opportunity to test).

1

I would do two things to get this behavior:

  1. It would include a Signalhandler to capture some signal from the operating system, such as USR1 (whereas vc is in a *UNIX). And whenever the process receives this signal, cause the sources to be reloaded.
    Signal.trap("USR1") do
      load "caminho_pro_arquivo_que_deve_ser_recarregado.rb"
    end
  1. Would use the watchr to monitor file system changes, so that whenever any of the sources change, it sends the signal USR1 for the process.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.