From: Rick DeNatale Date: 2008-03-09T21:50:59+09:00 Subject: Re: Redefine a class with same name On 3/9/08, ts wrote: > >>>>> "M" == Martin Boese writes: > > M> 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 > > M> class A < X > M> end > > Object.send(:remove_const, :A) > > M> class A < Y > M> 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 => # 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 # from (irb):15 irb(main):016:0> -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/