overwriting (for real) a class

Discussion in 'Ruby' started by Christopher J. Bottaro, Mar 3, 2008.

  1. 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.
     
    Christopher J. Bottaro, Mar 3, 2008
    #1
    1. Advertising

  2. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Mar 3, 2008
    #2
    1. Advertising

  3. Christopher J. Bottaro

    ThoML Guest

    ThoML, Mar 3, 2008
    #3
  4. Christopher J. Bottaro

    _why Guest

    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
     
    _why, Mar 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. senthil
    Replies:
    5
    Views:
    1,420
    senthil
    Jan 24, 2004
  2. Peter Grison
    Replies:
    3
    Views:
    546
    Roedy Green
    Apr 27, 2004
  3. Ole Nielsby
    Replies:
    3
    Views:
    354
    Ole Nielsby
    Oct 11, 2006
  4. Dom Bannon
    Replies:
    10
    Views:
    466
    Jorgen Grahn
    Oct 7, 2010
  5. paul
    Replies:
    10
    Views:
    214
    Tom Werner
    Nov 10, 2006
Loading...

Share This Page