[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Tim Morgan

2/26/2008 10:33:00 AM

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

5 Answers

Stefano Crocco

2/26/2008 10:41:00 AM

0

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

Joachim Glauche

2/26/2008 10:47:00 AM

0

Tim Morgan 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.
--
Posted via http://www.ruby-....

Klaus Schadenfreude

1/5/2013 8:31:00 PM

0

>"SaPeIsMa" <SaPeIsMa@Hotmail.com> wrote in talk.politics.guns :

>
>"Klaus Schadenfreude" <klausschadenfreude@yahoo.com> wrote in message
>news:2qjge8dhrpu49t83kl6bj5bc26saf5tuj6@4ax.com...
>> My neighbor built a fence.
>>
>> Unfortunately, the fence is ten feet on my side of the property line.
>> I told him that he is infringing on my property.
>>
>> He said it's OK, because I have plenty of property left.
>>
>
>1) Make sure that you know EXACTLY where the property line is
> If you can find any markers, that's even better.
> Place clear stakes at those locations.
> Better yet set up 2 directional pointers to the location deep enough
>on your property, that he would need to clearly trespass to move them
>2) Send him a registered letter demanding he move the fence, or you will
>remove it within 15 days, and demand compensation for the effort.
>3) 21 days later, take a chainsaw to the fence and send him the bill for
>your time.
>
>Oh, and send copies to the city/county clerk.

Um, I think you're missing the point.

Lookout

1/5/2013 11:40:00 PM

0

On Sat, 5 Jan 2013 10:55:51 -0600, "SaPeIsMa" <SaPeIsMa@Hotmail.com>
wrote:

>
>"Klaus Schadenfreude" <klausschadenfreude@yahoo.com> wrote in message
>news:2qjge8dhrpu49t83kl6bj5bc26saf5tuj6@4ax.com...
>> My neighbor built a fence.
>>
>> Unfortunately, the fence is ten feet on my side of the property line.
>> I told him that he is infringing on my property.
>>
>> He said it's OK, because I have plenty of property left.
>>
>
>1) Make sure that you know EXACTLY where the property line is
> If you can find any markers, that's even better.
> Place clear stakes at those locations.
> Better yet set up 2 directional pointers to the location deep enough
>on your property, that he would need to clearly trespass to move them
>2) Send him a registered letter demanding he move the fence, or you will
>remove it within 15 days, and demand compensation for the effort.
>3) 21 days later, take a chainsaw to the fence and send him the bill for
>your time.
>
>Oh, and send copies to the city/county clerk.
>
kraut doesn't own shit. He's making this up.

And your solution will get you into court and out of a lot of money

Klaus Schadenfreude

1/6/2013 12:54:00 PM

0

>Lookout <mrLookout1@yahoo.com> wrote in talk.politics.guns :

>On Sat, 5 Jan 2013 10:55:51 -0600, "SaPeIsMa" <SaPeIsMa@Hotmail.com>
>wrote:
>
>>
>>"Klaus Schadenfreude" <klausschadenfreude@yahoo.com> wrote in message
>>news:2qjge8dhrpu49t83kl6bj5bc26saf5tuj6@4ax.com...
>>> My neighbor built a fence.
>>>
>>> Unfortunately, the fence is ten feet on my side of the property line.
>>> I told him that he is infringing on my property.
>>>
>>> He said it's OK, because I have plenty of property left.
>>>
>>
>>1) Make sure that you know EXACTLY where the property line is
>> If you can find any markers, that's even better.
>> Place clear stakes at those locations.
>> Better yet set up 2 directional pointers to the location deep enough
>>on your property, that he would need to clearly trespass to move them
>>2) Send him a registered letter demanding he move the fence, or you will
>>remove it within 15 days, and demand compensation for the effort.
>>3) 21 days later, take a chainsaw to the fence and send him the bill for
>>your time.
>>
>>Oh, and send copies to the city/county clerk.
>>
>kraut doesn't own shit. He's making this up.

You're guessing.

_____________
"Again, you're guess[sic]. That's lying." -Look-Guessing is
Lying-out"
Message-ID: <3iha06tfmrskb5jsbu2r5nlibqnj5u3qte@4ax.com>

"But you tried to imply. That's lying." -Look-Implying is Lying-Out"
Message-ID: <1s15j65bthnpqu89ltrgep41tb0ku86c0l@4ax.com>