It is possible to unload a class?


#1

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


#2

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.


#3

Is it possible to reload a class in irb?


#4

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


#5

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

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.


#6

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


#7

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

#8

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


#9

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