Object.send(:remove_const, :Polygon)

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

from (irb):9

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

···

from :0

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
        from (irb):9
        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???

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"

···

On Dec 30, 2009, at 5:47 PM, Ralph Shnelvar 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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Object.send(:remove_const, :Polygon) </title>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
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.

···

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