[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

overwriting (for real) a class

Christopher J. Bottaro

3/3/2008 7:23:00 PM

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.

3 Answers

Sebastian Hungerecker

3/3/2008 7:31:00 PM

0

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.
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

ThoML

3/3/2008 7:33:00 PM

0

> 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/0fc850...

_why

3/3/2008 7:49:00 PM

0

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