# Overwriting (for real) a class

**URL:** <https://www.ruby-forum.com/t/overwriting-for-real-a-class/130840>\
**Category:** Ruby\
**Created:** [March 3, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/overwriting-for-real-a-class/130840 "2008-03-03T20:24:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [March 3, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/overwriting-for-real-a-class/130840/1 "2008-03-03T20:24:39Z")

</div>

How can completely overwrite a class (not extend it)? For example.

fileA.rb:  
class MyClass  
def foo  
end  
end

fileB.rb (executed after fileA.rb):  
class MyClass  
def bar  
end  
end

MyClass.new.foo # I want a no method exception here

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [March 3, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/overwriting-for-real-a-class/130840/2 "2008-03-03T20:32:21Z")

</div>

Christopher J. Bottaro wrote:

> How can completely overwrite a class (not extend it)?

MyClass = nil  
class MyClass  
…  
end

Though you will get a warning for reassigning a constant. Or you could  
do:  
Object.send(:remove\_const, :MyClass)  
class MyClass  
…  
end

HTH,  
Sebastian.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [March 3, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/overwriting-for-real-a-class/130840/3 "2008-03-03T20:37:04Z")

</div>

> MyClass.new.foo # I want a no method exception here

You can remove methods (there is a private method Class#remove\_method)  
or remove the constant MyClass[1] and then define it anew. What would  
be a sensible use case for this?

Thomas.

[1] Example:  
[http://groups.google.com/group/ruby-talk-google/msg/0fc850e23243d830](http://groups.google.com/group/ruby-talk-google/msg/0fc850e23243d830)

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [March 3, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/overwriting-for-real-a-class/130840/4 "2008-03-03T20:49:34Z")

</div>

On Tue, Mar 04, 2008 at 04:34:56AM +0900, ThoML wrote:

> You can remove methods (there is a private method Class#remove\_method)  
> or remove the constant MyClass[1] and then define it anew. What would  
> be a sensible use case for this?

For security on Try Ruby, I remove any IO classes and methods. Some  
are replaced with a mock filesystem even.

\_why
