# Deleting a class

**URL:** <https://rubytalk.org/t/deleting-a-class/3539>\
**Category:** ruby-talk\
**Created:** [11 December 2002 18:20 UTC](https://rubytalk.org/t/deleting-a-class/3539 "2002-12-11T18:20:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [11 December 2002 18:20 UTC](https://rubytalk.org/t/deleting-a-class/3539/1 "2002-12-11T18:20:01Z")

</div>

Hello,

I’m try to find a way to completely delete a class. (What I would  
eventually like is a way to load an entire GUI by default, but if the user  
requests a new one, I’d like to unload the old one and load up the new one.)

So, in this code I create class Foo, but I can’t figure out how to delete  
it. Am I doing the GC wrong, or is there some reference to Foo I haven’t  
yet deleted? Or both?

def getAllClasses  
classes = []  
ObjectSpace.each\_object (Class) do |klass|  
classes \<\< klass.to\_s  
end  
classes  
end

def getAllConstants  
Module.constants  
end

originalClasses = getAllClasses  
originalConstants = getAllConstants

class Foo  
end

Object.module\_eval { remove\_const ‘Foo’ }

GC.start

puts 'New classes: ‘+(getAllClasses - originalClasses ).join(’, ')  
puts 'New constants: ‘+(getAllConstants - originalConstants).join(’, ')

Thanks for any help you can offer,

Chris

---

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [11 December 2002 18:51 UTC](https://rubytalk.org/t/deleting-a-class/3539/2 "2002-12-11T18:51:19Z")

</div>

Here’s something weird:

If instead of using the code:

class Foo  
end

…I instead use…

eval ‘class Foo; end’

…then the rest of the code _does_ delete the class!

I just went through all of that in my mail “all the pretty evals…”; I was  
asking if these are the same. Obviously they aren’t the same.

So what’s the difference? And what about the other evals: do they work  
like ‘eval’ does? What about ‘require’ and ‘load’? (Those last two were  
rhetorical questions; I can figure it out on my own. I just want to know  
what the difference is and why it works this way.)

Chris

> **···**
>
> ----- Original Message -----  
> From: “Chris Pine” [nemo@hellotree.com](mailto:nemo@hellotree.com)  
> Newsgroups: comp.lang.ruby  
> Sent: Wednesday, December 11, 2002 10:20 AM  
> Subject: deleting a class
> 
> Hello,
> 
> I’m try to find a way to completely delete a class. (What I would  
> eventually like is a way to load an entire GUI by default, but if the user  
> requests a new one, I’d like to unload the old one and load up the new one.)
> 
> So, in this code I create class Foo, but I can’t figure out how to delete  
> it. Am I doing the GC wrong, or is there some reference to Foo I haven’t  
> yet deleted? Or both?
> 
> def getAllClasses  
> classes = []  
> ObjectSpace.each\_object (Class) do |klass|  
> classes \<\< klass.to\_s  
> end  
> classes  
> end
> 
> def getAllConstants  
> Module.constants  
> end
> 
> originalClasses = getAllClasses  
> originalConstants = getAllConstants
> 
> class Foo  
> end
> 
> Object.module\_eval { remove\_const ‘Foo’ }
> 
> GC.start
> 
> puts 'New classes: ‘+(getAllClasses - originalClasses ).join(’, ')  
> puts 'New constants: ‘+(getAllConstants - originalConstants).join(’, ')
> 
> Thanks for any help you can offer,
> 
> Chris

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [12 December 2002 11:36 UTC](https://rubytalk.org/t/deleting-a-class/3539/3 "2002-12-12T11:36:07Z")

</div>

> ...then the rest of the code \*does\* delete the class!

Well, you are just seeing that the GC is conservative.

Imagine that something write

&nbsp;&nbsp;&nbsp;class A  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;a = A.new  
&nbsp;&nbsp;&nbsp;Object.module\_eval { remove\_const 'A' }

at this step, ruby can't remove completely the class because this class is  
referenced by the object `a'

pigeon% cat b.rb  
#!/usr/bin/ruby  
class A  
&nbsp;&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "a"  
&nbsp;&nbsp;&nbsp;end  
end

a = A.new

Object.module\_eval { remove\_const 'A' }  
class A  
&nbsp;&nbsp;&nbsp;def b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "b"  
&nbsp;&nbsp;&nbsp;end  
end

b = A.new  
a.a  
b.b  
b.a  
pigeon%

pigeon% b.rb  
a  
b  
./b.rb:20: undefined method `a' for #\<A:0x401ad338\> (NameError)  
pigeon%

This is something similar in your case, because it exist a \*temporary\*  
variable which still make reference to the class

> I just went through all of that in my mail "all the pretty evals..."; I was  
> asking if these are the same. Obviously they aren't the same.

Well apparently there is another problem 🙂

Just try this

pigeon% ruby -e 'class A; end; A.module\_eval "12"'  
pigeon%

pigeon% ruby -e '.module\_eval "12"'  
-e:1: undefined method `module\_eval' for :Array (NameError)  
pigeon%

#module\_eval is a method defined in Module, and only object which inherit  
&nbsp;&nbsp;from Module can access this method.

Now

pigeon% ruby -e 'class A; end; A.instance\_eval "12"'  
pigeon%

pigeon% ruby -e '.instance\_eval "12"'  
pigeon%

#instance\_eval is defined in Kernel and \*all\* objects can use this method.

You have 2 methods with differents names and defined in 2 differents  
classes.

Now

pigeon% ruby -e 'class A; end; A.eval "12"'  
-e:1: private method `eval' called for A:Class (NameError)  
pigeon%

pigeon% ruby -e '.eval "12"'  
-e:1: private method `eval' called for :Array (NameError)  
pigeon%

#eval is a module function of Kernel

Now to see the difference

pigeon% cat b.rb  
#!/usr/bin/ruby  
class A  
&nbsp;&nbsp;&nbsp;def A.a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "A::a"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "A#a"  
&nbsp;&nbsp;&nbsp;end  
end

def a  
&nbsp;&nbsp;&nbsp;puts "Object#a"  
end

x = A.new

eval 'p self; a'

x.instance\_eval 'p self; a'

class A  
&nbsp;&nbsp;&nbsp;eval 'p self; a'  
end

A.instance\_eval 'p self; a'  
pigeon%

pigeon% b.rb  
main  
Object#a  
#\<A:0x401ad1e4\>  
A#a  
A  
A  
pigeon%

Guy Decoux

> **···**
>
> A::a  
> A::a

---

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [12 December 2002 16:23 UTC](https://rubytalk.org/t/deleting-a-class/3539/4 "2002-12-12T16:23:46Z")

</div>

I don’t understand. Are you saying there’s a temporary reference to the  
class? I was asking why that would be the case when I define a class one  
way, but not another?

This code does NOT remove the class Foo:

class Foo; end  
Object.module\_eval { remove\_const ‘Foo’ }  
GC.start

But this DOES remove it:

eval 'class Foo; end’  
Object.module\_eval { remove\_const ‘Foo’ }  
GC.start

To sum up all the ways of defining a class I can think of, the following  
ways of defining a class WILL allow the class to be removed (at least by  
removing the constant and starting the GC):

–\> eval ‘class Foo; end’  
–\> Foo = Class.new  
–\> load ‘foo.rb’  
–\> require ‘foo.rb’

(foo.rb is simply: class Foo; end)

And the following WILL NOT allow the class to be removed:

–\> class Foo; end  
–\> eval ‘Foo = Class.new’ # How weird is that??

So I just wanted to know why. If you are saying there’s a temporary  
reference, why is it avoided by using ‘eval’ (or sometimes _caused_ by using  
’eval’)? Is there a way I can get rid of this temporary reference?

Chris

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [12 December 2002 16:32 UTC](https://rubytalk.org/t/deleting-a-class/3539/5 "2002-12-12T16:32:20Z")

</div>

> I don't understand. Are you saying there's a temporary reference to the  
> class?

Yes,

> This code does NOT remove the class Foo:

> &nbsp;&nbsp;class Foo; end  
> &nbsp;&nbsp;Object.module\_eval { remove\_const 'Foo' }  
> &nbsp;&nbsp;GC.start

In this case, some where on the stack, it exist an object which reference  
Foo (this can be a node, or anything else)

> But this DOES remove it:

> &nbsp;&nbsp;eval 'class Foo; end'  
> &nbsp;&nbsp;Object.module\_eval { remove\_const 'Foo' }  
> &nbsp;&nbsp;GC.start

Your code is just different, and in this case no object reference Foo

> So I just wanted to know why. If you are saying there's a temporary  
> reference, why is it avoided by using 'eval' (or sometimes \*caused\* by using  
> 'eval')? Is there a way I can get rid of this temporary reference?

no : this is why the GC is called "conservative". It can mark object, that  
normally don't exist but it's not important. When you write

&nbsp;&nbsp;&nbsp;class Foo; end  
&nbsp;&nbsp;&nbsp;Object.module\_eval { remove\_const 'Foo' }

Now it's not possible to access the class Foo.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [12 December 2002 17:15 UTC](https://rubytalk.org/t/deleting-a-class/3539/6 "2002-12-12T17:15:13Z")

</div>

When you write

class Foo; end  
Object.module\_eval { remove\_const ‘Foo’ }

Now it’s not possible to access the class Foo.

> **···**
>
> ## ----- Original Message -----
> 
> Sure it is:
> 
> def getAllClasses  
> classes = []  
> ObjectSpace.each\_object (Module) do |klass|  
> classes \<\< klass.inspect  
> end  
> classes  
> end
> 
> originalClasses = getAllClasses
> 
> class Foo; end
> 
> Object.module\_eval { remove\_const ‘Foo’ }
> 
> GC.start
> 
> deletedClasses = []  
> ObjectSpace.each\_object (Module) do |klass|  
> deletedClasses \<\< klass if(!originalClasses.include?(klass.inspect))  
> end
> 
> foo = deletedClasses[0].new
> 
> p foo # --\> #Foo:0x2774c30
> 
> Note that if you change
> 
> class Foo; end
> 
> to
> 
> eval ‘class Foo; end’
> 
> you’ll get a NoMethodError, since the class is deleted. This proves that my  
> code isn’t holding a reference to the class.
> 
> Chris

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [12 December 2002 17:28 UTC](https://rubytalk.org/t/deleting-a-class/3539/7 "2002-12-12T17:28:42Z")

</div>

> &nbsp;&nbsp;class Foo; end

pigeon% cat b.rb  
#!/usr/bin/ruby  
def getAllClasses  
&nbsp;&nbsp;&nbsp;classes =   
&nbsp;&nbsp;&nbsp;ObjectSpace.each\_object (Module) do |klass|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;classes \<\< klass.inspect  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;classes  
end

originalClasses = getAllClasses  
class Object  
&nbsp;&nbsp;&nbsp;class Foo; end  
end

Object.module\_eval { remove\_const 'Foo' }

GC.start

deletedClasses =   
ObjectSpace.each\_object (Module) do |klass|  
&nbsp;&nbsp;&nbsp;deletedClasses \<\< klass if(!originalClasses.include?(klass.inspect))  
end

foo = deletedClasses[0].new  
p foo  
pigeon%

pigeon% b.rb  
./b.rb:24: undefined method `new' for nil (NameError)  
pigeon%

> you'll get a NoMethodError, since the class is deleted. This proves that my  
> code isn't holding a reference to the class.

This prove nothing.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [12 December 2002 18:08 UTC](https://rubytalk.org/t/deleting-a-class/3539/8 "2002-12-12T18:08:05Z")

</div>

Are you saying I do have a reference to Foo in my code?! There might be a  
reference to Foo somewhere, but it ain’t in my code.

You said I couldn’t get the object back. I could. But this is all  
tangential to my original question: “How are these class definitions  
different?”

Your answer seems to be: “They are different internally to Ruby.”

Well, I knew that. 🙂 If there’s no principle behind _why_ these are  
different, fine. I was just wondering if there was something there which I  
didn’t understand (and if there was any way I could actually delete a class  
no matter how it was created, which you weren’t able to do, either).

I have come to expect everything to make sense, to be intentional in Ruby;  
but I think I am getting to the point where I am noticing implementation  
specifics, rather than language specifics, which are not so easily  
explained.

Anyway, thank you for your responses,

Chris
