# It is possible to unload a class?

**URL:** <https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770>\
**Category:** Ruby\
**Created:** [January 24, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770 "2006-01-24T20:00:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/1 "2006-01-24T20:00:59Z")

</div>

Hi,

we can load a library, a class or a gem with the funtionc “require”. If  
I want to unload a library, a class or a gem, how can I do this, (or it  
is possible?)

Thanks you very much

sayoyo

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/2 "2006-01-24T21:13:44Z")

</div>

On Wed, Jan 25, 2006 at 03:58:12AM +0900, removed\_email\_address@domain.invalid wrote:

> we can load a library, a class or a gem with the funtionc “require”. If  
> I want to unload a library, a class or a gem, how can I do this, (or it  
> is possible?)

In general it’s not possible.

I suppose you could try to hunt down and rebind every reference to the  
classes/modules in the library, eventually allowing them to be garabge  
collected. But I can’t think of a practical reason to do that.

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/3 "2006-01-24T21:44:06Z")

</div>

Is it possible to reload a class in irb?

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/4 "2006-01-24T22:05:21Z")

</div>

You could try something with continuations? Go back to the original  
state if you want to unload a library. You’ll lose other things too,  
unfortunately.

Jules

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/5 "2006-01-24T22:14:26Z")

</div>

On Jan 24, 2006, at 4:05 PM, Jules J. wrote:

> You could try something with continuations? Go back to the original  
> state if you want to unload a library. You’ll lose other things too,  
> unfortunately.
> 
> Jules
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Continuations aren’t that magical. They don’t go back in time, just  
space as it were. IOW, if you can’t do it with first class functions  
(possibly using CPS) (or catch/throw or exceptions (although  
exceptions for flow control is evil)), you probably can’t do it with  
continuations either. Of course certain things may be easier to  
express with continuations. Continuations are really just glorified  
gotos.

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 24, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/6 "2006-01-24T22:11:24Z")

</div>

Kelly Dwight F. wrote:

> Is it possible to reload a class in irb?

If it’s from another file, you can use

irb\>load ‘someclass.rb’

instead of require.

-Justin

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 25, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/7 "2006-01-25T15:04:34Z")

</div>

Edward F. schrieb:

> I suppose you could try to hunt down and rebind every reference to the  
> classes/modules in the library, eventually allowing them to be garabge  
> collected. But I can’t think of a practical reason to do that.

Well, there is at least one practical reason I can think of. Consider  
a server running several services. Theses services may be started/loaded  
and stopped/unloaded during runtime. Thus, it would be really nice to  
get rid of classes or modules, which belong to an old service which was  
stopped.

Another aspect in this topic is: Is it possible to get rid of a mix-in  
which was introduced to a class? I mean, you have a class definition and  
afterwards you add a mix-in to that class. At some point in the future,  
you don’t need the functionality of the mix-in and want to get rid of  
it. Maybe it is possible with undef or something like that?

- Steffen

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 26, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/8 "2006-01-26T17:06:24Z")

</div>

On Wed, Jan 25, 2006 at 03:58:12AM +0900, removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> we can load a library, a class or a gem with the funtionc “require”. If  
> I want to unload a library, a class or a gem, how can I do this, (or it  
> is possible?)

See [ruby-talk:161691] and the following thread (in particular,  
[ruby-talk:161865]).

Paul

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [January 26, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/it-is-possible-to-unload-a-class/96770/9 "2006-01-26T17:15:26Z")

</div>

I haven’t looked at the source to Webrick, but it seems to me that it  
is doing this exact thing. For each request it somehow unloads and  
reloads the entire application. If it didn’t then your changes between  
requests would not take effect. Please enlighten us on how this magic  
happens.

Troy
