Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > overwriting (for real) a class

Reply
Thread Tools

overwriting (for real) a class

 
 
Christopher J. Bottaro
Guest
Posts: n/a
 
      03-03-2008
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.

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      03-03-2008
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
 
 
 
ThoML
Guest
Posts: n/a
 
      03-03-2008
> 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-...c850e23243d830
 
Reply With Quote
 
_why
Guest
Posts: n/a
 
      03-03-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overwriting an entire class from a disk record Dom Bannon C++ 10 10-07-2010 09:23 AM
Overwriting the Integer class for method succ! (instead of just succ) paul Ruby 10 11-10-2006 07:29 PM
Self-overwriting class - is 'volatile' needed for field keepers? Ole Nielsby C++ 3 10-11-2006 01:20 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
overwriting fields from abstract class Peter Grison Java 3 04-27-2004 05:14 PM



Advertisments