overwriting (for real) a class

  • Thread starter Christopher J. Bottaro
  • Start date
C

Christopher J. Bottaro

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.
 
S

Sebastian Hungerecker

Christopher said:
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.
 
W

_why

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top