Hot-swapping classes using Kernel.load -- catching excptns

In my Ruby script, I’d like to be able to hotswap classes. The best way
I’ve found to do this thus far is with the Kernel.load method:

load ‘class_file.rb’

It’s not perfect – it can’t redefine constants, for instance, but it
mostly works. The problem comes when my environment is somehow
different, such that hotswapping the class is no longer an option.
Let’s say class_file.rb looks like this:

require ‘another_file’
class MyClass; end

Now let’s say I move another_file.rb somewhere else, and then decide to
hotswap MyClass with the load method. This will generate a LoadError
because that file no longer exists. Okay, not a problem – we simply
catch that exception and let it die silently. No need to take down the
whole process just because one class couldn’t be hotswapped:

begin
load ‘class_file.rb’
rescue
end

The problem is, this doesn’t actually catch the LoadError! If you run
this code it will die with an exception in any case.

The solution (or hack anyway) is to spin this off into a thread:

Thread.new { load ‘class_file.rb’ }

It’s a bit of overhead for simply reloading a Ruby file, and may open up
another can of worms in single-threaded applications, but it does work.

Anyone else written hot-swappable Ruby classes? Let me know your
thoughts.

Alle Tuesday 26 February 2008, Tim M. ha scritto:

require ‘another_file’
rescue
another can of worms in single-threaded applications, but it does work.

Anyone else written hot-swappable Ruby classes? Let me know your
thoughts.

I can’t help you with hot-swappable classes, but I can tell you because
your
code fails to catch the LoadError. This happens because you use rescue
without
a class. When you do this, you will catch only exceptions descending
from (if
I’m not mistaken) StandardError. LoadError doesn’t descend from
StandardError,
so it isn’t caught. If you replace rescue with

rescue LoadError

your code should work.

I hope this helps

Stefano

Tim M. wrote:

In my Ruby script, I’d like to be able to hotswap classes. The best way
I’ve found to do this thus far is with the Kernel.load method:

load ‘class_file.rb’

It’s not perfect – it can’t redefine constants, for instance, but it
mostly works. The problem comes when my environment is somehow
different, such that hotswapping the class is no longer an option.
Let’s say class_file.rb looks like this:

Try
Object.send :remove_const, :Name

Note that existing instances of Name won’t be destroyed. You have to
create a new instance to work with the new stuff.