From: Tim Morgan Date: 2008-02-26T19:33:18+09:00 Subject: 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. -- Posted via http://www.ruby-forum.com/.