Ruby application that keeps reloading source files

5

How can I design my application so that whenever I modify and save a .rb file, the application behaves "in the new way"?

I'm doing an application that reads the command, executes, prints the result and reads the next command (REPL, Read-Eval-Print-Loop). And I'd like the fonts to be reloaded whenever I change them.

More or less the way Rails and Unity (the graphics engine) do when I change the scripts.

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

    
asked by anonymous 17.01.2014 / 13:03

2 answers

9

You can use the gem rerun . Install with:

gem install rerun

And if your program is called script.rb , run with:

rerun script.rb

It will monitor the file system and replay the command whenever a file changes. The only caution to take into account is that it kills the previous process, so you may end up corrupting some configuration file (good programs should be resilient against this, it's a good opportunity to test).

    
17.01.2014 / 14:07
1

I would do 2 things to get this behavior:

  • Include a SignalHandler to catch some signal from the operating system, such as USR1 (assuming you are on 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
    
  • Use watchr to monitor changes to the file system, so that whenever any of the sources changes, it sends the USR1 sign for the process.
  • 23.01.2014 / 15:42