Last Updated: February 25, 2016
·
4.866K
· vjt

Reload a single gem dependency source file

You are inside the Rails console, developing the app and a corresponding framework located in a separate gem.

You have a symlink in your code directory pointing to the gem source tree in $GEM_HOME (or, even better, you are using my bundler trick ;-)). Anyway - your gem source files are reachable via ../gemname/lib/gem_name/ relative to your app.

If you change the gem source, you'd have to reboot the whole Rails console - losing at least 10 seconds for every change.

Not anymore - just use Kernel.load:

>> load '../gemname/lib/gem_name/changed_file.rb'
>> reload!

Why does this work? Because Rails' reload! helper undefines all autoloaded constants, and then reloads your source files - and when your sources are reloaded, any metaprogramming carried out by your gem will be invoked again, re-defining methods and stuff inside your classes.

Danger, Will Robinson

  • if you do nasty(tm) things in your gem such as altering the class hierarchy, you'll get superclass mismatch.
  • if you don't do metaprogramming in your gem, you can skip the reload! - as the gem's modules and classes will be updated by the first load call
  • if you define constants in the source you are reloading, you'll get warnings - of course.

Bottom Line

Ruby is truly dynamic - I like to think about it as an UNIX system: very seldom it needs to be rebooted: there is always a way to fix things avoiding a cold restart - and learning how to do it is much fun :-).

~vjt