Redefine a class with same name

Is there any way to to remove the definition of a class so I can reuse its
name? Like below I'd like to reuse the name 'A' but since it's already
defined with a different superclass I get a 'TypeError: superclass mismatch
for class A'.

···

------------------------------------------------------

class X
end
class Y
end

class A < X
end

==> Here I'd like to remove A's definition so I can:

class A < Y
end

==> without getting a: 'TypeError: superclass mismatch for class A'

------------------------------------------------------

Thanks,
Martin

Is there any way to to remove the definition of a class so I can reuse its
name? Like below I'd like to reuse the name 'A' but since it's already
defined with a different superclass I get a 'TypeError: superclass mismatch
for class A'.

Just remove the constant

class A < X
end

   Object.send(:remove_const, :A)

class A < Y
end

Guy Decoux

Keep in mind though, that this only removes the binding between the
constant and the class, it doesn't actually allow you to change the
old class, and any existing instances will still be instances of the
old class not the new one:

k$ irb
irb(main):001:0> class A
irb(main):002:1> def foo
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> a = A.new
=> #<A:0x6e578>
irb(main):006:0> a.foo
=> nil
irb(main):007:0> Object.send(:remove_const, :A)
=> A
irb(main):008:0> a.foo
=> nil
irb(main):009:0> a.class
=> A
irb(main):010:0> A
NameError: uninitialized constant A
        from (irb):10
irb(main):011:0> class A
irb(main):012:1> def bar
irb(main):013:2> end
irb(main):014:1> end
=> nil
irb(main):015:0> a.bar
NoMethodError: undefined method `bar' for #<A:0x6e578>
        from (irb):15
irb(main):016:0>

···

On 3/9/08, ts <decoux@moulon.inra.fr> wrote:

> Is there any way to to remove the definition of a class so I can reuse its
> name? Like below I'd like to reuse the name 'A' but since it's already
> defined with a different superclass I get a 'TypeError: superclass mismatch
> for class A'.

  Just remove the constant

> class A < X
> end

   Object.send(:remove_const, :A)

> class A < Y
> end

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Excellent! That's fine for me. I only have the case for a web framework of
mine when using with fastcgi as different calls might use same class names
which I can unregister now.

Thanks,
martin

···

On Sunday 09 March 2008 13:50:59 Rick DeNatale wrote:

On 3/9/08, ts <decoux@moulon.inra.fr> wrote:

>
> > Is there any way to to remove the definition of a class so I can
> reuse its M> name? Like below I'd like to reuse the name 'A' but since
> it's already M> defined with a different superclass I get a 'TypeError:
> superclass mismatch M> for class A'.
>
> Just remove the constant
>
> > class A < X
> > end
>
> Object.send(:remove_const, :A)
>
> > class A < Y
> > end

Keep in mind though, that this only removes the binding between the
constant and the class, it doesn't actually allow you to change the
old class, and any existing instances will still be instances of the
old class not the new one:

k$ irb
irb(main):001:0> class A
irb(main):002:1> def foo
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> a = A.new
=> #<A:0x6e578>
irb(main):006:0> a.foo
=> nil
irb(main):007:0> Object.send(:remove_const, :A)
=> A
irb(main):008:0> a.foo
=> nil
irb(main):009:0> a.class
=> A
irb(main):010:0> A
NameError: uninitialized constant A
        from (irb):10
irb(main):011:0> class A
irb(main):012:1> def bar
irb(main):013:2> end
irb(main):014:1> end
=> nil
irb(main):015:0> a.bar
NoMethodError: undefined method `bar' for #<A:0x6e578>
        from (irb):15
irb(main):016:0>