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

T

Tim Morgan

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.
 
S

Stefano Crocco

Alle Tuesday 26 February 2008, Tim Morgan ha scritto:
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.

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
 
J

Joachim Glauche

Tim said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top