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

**URL:** <https://www.ruby-forum.com/t/hot-swapping-classes-using-kernel-load-catching-excptns/130203>\
**Category:** Ruby\
**Created:** [February 26, 2008, 11:33am UTC](https://www.ruby-forum.com/t/hot-swapping-classes-using-kernel-load-catching-excptns/130203 "2008-02-26T11:33:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [February 26, 2008, 11:33am UTC](https://www.ruby-forum.com/t/hot-swapping-classes-using-kernel-load-catching-excptns/130203/1 "2008-02-26T11:33:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [February 26, 2008, 11:41am UTC](https://www.ruby-forum.com/t/hot-swapping-classes-using-kernel-load-catching-excptns/130203/2 "2008-02-26T11:41:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [February 26, 2008, 11:46am UTC](https://www.ruby-forum.com/t/hot-swapping-classes-using-kernel-load-catching-excptns/130203/3 "2008-02-26T11:46:54Z")

</div>

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.
