Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Last active January 4, 2016 21:09

Revisions

  1. garybernhardt revised this gist Jan 29, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@
    Object.send(:remove_const, :Sim)
    end

    # Instruct ruby to "unrequire" all of the gems files.
    # Instruct ruby to "unrequire" all of the simulator's files.
    # CAREFUL: make sure this only matches your library's files.
    sim_base = File.expand_path(File.join(File.dirname(__FILE__), '../../lib/sim'))
    $".delete_if do |s|
    @@ -28,6 +28,6 @@

    # Re-require your library
    # Note: because we removed all files previously required they will be reloaded
    # even if you didn't use load/autoload in your gem.
    # even if you didn't use load/autoload in your library.
    require_relative "../../lib/sim"
    end
  2. garybernhardt revised this gist Jan 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Goal: put a non-Rails-aware Ruby library using normal `require`s in
    # lib/mything. Have it transparently reloaded between requests like Rails app
    # lib/sim. Have it transparently reloaded between requests like Rails app
    # code is.
    #
    # The code here goes inside of your configure block in
  3. garybernhardt revised this gist Jan 29, 2014. 1 changed file with 5 additions and 11 deletions.
    16 changes: 5 additions & 11 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -5,18 +5,12 @@
    # The code here goes inside of your configure block in
    # config/environments/development.rb. There are two parts, commented inline.

    # Disable reload_classes_only_on_change. This makes Rails invoke the reloader
    # even if no source files are changed. This is necessary because the autoloader
    # and reloader don't know about our library, so they won't know to trigger a
    # reload when they change. This will probably become slow for very large apps;
    # on my (currently very small) app it costs about 85 ms in total request time.
    config.reload_classes_only_on_change = false
    # Reload code whenever the simulator changes.
    config.watchable_dirs["lib/sim"] = [:rb]
    config.watchable_files << "lib/sim.rb"

    # Manually unload and reload the library before every request. If it has global
    # state, this will almost certainly fall over in terrible ways. If it defines
    # constants other than the one ("Sim") explicitly dealt with here, you may
    # actually die in real life. This assumes that there's a lib/sim.rb loading
    # files from lib/sim/.
    # Manually unload and reload the simulator before every request. This assumes
    # that there's a lib/sim.rb loading files from lib/sim/.
    ActionDispatch::Reloader.to_prepare do
    # If the library's top level module is currently loaded, unload it
    if Object.const_defined?(:Sim)
  4. garybernhardt created this gist Jan 28, 2014.
    39 changes: 39 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    # Goal: put a non-Rails-aware Ruby library using normal `require`s in
    # lib/mything. Have it transparently reloaded between requests like Rails app
    # code is.
    #
    # The code here goes inside of your configure block in
    # config/environments/development.rb. There are two parts, commented inline.

    # Disable reload_classes_only_on_change. This makes Rails invoke the reloader
    # even if no source files are changed. This is necessary because the autoloader
    # and reloader don't know about our library, so they won't know to trigger a
    # reload when they change. This will probably become slow for very large apps;
    # on my (currently very small) app it costs about 85 ms in total request time.
    config.reload_classes_only_on_change = false

    # Manually unload and reload the library before every request. If it has global
    # state, this will almost certainly fall over in terrible ways. If it defines
    # constants other than the one ("Sim") explicitly dealt with here, you may
    # actually die in real life. This assumes that there's a lib/sim.rb loading
    # files from lib/sim/.
    ActionDispatch::Reloader.to_prepare do
    # If the library's top level module is currently loaded, unload it
    if Object.const_defined?(:Sim)
    Object.send(:remove_const, :Sim)
    end

    # Instruct ruby to "unrequire" all of the gems files.
    # CAREFUL: make sure this only matches your library's files.
    sim_base = File.expand_path(File.join(File.dirname(__FILE__), '../../lib/sim'))
    $".delete_if do |s|
    is_in_sim_dir = s.start_with?(sim_base + "/")
    is_sim_file = s == sim_base + ".rb"
    is_in_sim_dir || is_sim_file
    end

    # Re-require your library
    # Note: because we removed all files previously required they will be reloaded
    # even if you didn't use load/autoload in your gem.
    require_relative "../../lib/sim"
    end