# Object.send(:remove\_const, :Polygon)

**URL:** <https://www.ruby-forum.com/t/object-send-remove-const-polygon/180877>\
**Category:** Ruby\
**Created:** [December 30, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/object-send-remove-const-polygon/180877 "2009-12-30T23:47:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [December 30, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/object-send-remove-const-polygon/180877/1 "2009-12-30T23:47:35Z")

</div>

Object.send(:remove\_const, :Polygon)

Newbie here.

F:\InstantRails-2.0-win\rails\_apps\>irb -v

irb 0.9.5(05/04/13)

F:\InstantRails-2.0-win\rails\_apps\>ruby -v

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Consider

irb(main):001:0\> class X

irb(main):002:1\> end

=\> nil

irb(main):003:0\>

irb(main):004:0\* class Y \< X

irb(main):005:1\> end

=\> nil

irb(main):006:0\>

irb(main):007:0\* Object.send(:remove\_const, :X)

=\> X

irb(main):008:0\>

irb(main):009:0\* X.class

NameError: uninitialized constant X

&nbsp; &nbsp; &nbsp; &nbsp; from (irb):9

&nbsp; &nbsp; &nbsp; &nbsp; from :0

irb(main):010:0\> Y.class

=\> Class

irb(main):011:0\> Y.superclass

=\> X

irb(main):012:0\> Y.superclass.superclass

=\> Object

irb(main):013:0\>

How can the class X still exist when it has been removed?????

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [December 31, 2009, 12:01am UTC](https://www.ruby-forum.com/t/object-send-remove-const-polygon/180877/2 "2009-12-31T00:01:04Z")

</div>

On Dec 30, 2009, at 5:47 PM, Ralph S. wrote:

> irb(main):006:0\>  
> =\> X  
> irb(main):012:0\> Y.superclass.superclass  
> =\> Object  
> irb(main):013:0\>
> 
> How can the class X still exist when it has been removed???

remove\_const doesn’t destroy the class object it just deletes the  
binding represented by the constant.

Ruby arranges for a class to have a name (a string label) that matches  
the first constant that the class is bound to. The name still remains  
even if the constant binding is removed (via remove\_const).

?\> a = Class.new  
=\> #Class:0x124b6ec

> > a.name  
> > =\> “”
> 
> > Foobar = a  
> > =\> Foobar
> 
> > a.name  
> > =\> “Foobar”
> 
> > Object.send(:remove\_const, :Foobar)  
> > =\> Foobar
> 
> > Foobar  
> > NameError: uninitialized constant Foobar  
> > from (irb):31
> 
> > a.name  
> > =\> “Foobar”
> 
> > a.inspect  
> > =\> “Foobar”

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [December 31, 2009, 4:10am UTC](https://www.ruby-forum.com/t/object-send-remove-const-polygon/180877/3 "2009-12-31T04:10:27Z")

</div>

On Wednesday 30 December 2009 04:47:08 pm Ralph S. wrote:

> Newbie here.

Hi, welcome. Please turn off HTML mail, or at least configure it to send  
both  
HTML and text. When I opened your email, this is what I saw:

Object.send(:remove\_const, :Polygon) \<!-- body { margin: 5px 5px 5px 5px; background-color: #ffffff; } /\* ========== Text Styles ========== \*/ ...

Pages and pages of that, unless I re-enable HTML mail.

So…

> irb(main):007:0\* Object.send(:remove\_const, :X)  
> =\> X  
> irb(main):011:0\> Y.superclass  
> =\> X

> How can the class X still exist when it has been removed???

It hasn’t. Let me put it another way:

irb(main):001:0\> x = “foo”  
=\> “foo”  
irb(main):002:0\> y = x  
=\> “foo”  
irb(main):003:0\> x = nil  
=\> nil  
irb(main):004:0\> y  
=\> “foo”

How can the string “foo” still exist when it has been removed???

In Ruby, a constant is more or less just a variable that we’ve agreed  
shouldn’t change. And variables refer to objects, and classes are  
objects,  
too. So as long as Y exists, X does, too.

That said, I ran some experiments with finalizers, and maybe it’s a  
quirk in  
the garbage collection, but I can’t actually figure out when a class is  
actually removed.
