Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:13:38+00:00 2026-06-07T20:13:38+00:00

Undefining a method in Ruby is pretty simple, I can just use undef METHOD_NAME

  • 0

Undefining a method in Ruby is pretty simple, I can just use undef METHOD_NAME.

Is there anything similar for a class? I am on MRI 1.9.2.

I have to undefine an ActiveRecord Model, run two lines of code, and restore the model back to its original form.

The problem is, I have an model Contact and I am using a company’s API and it happens that they have some class called Contact, and changing my model name would be lot of work for me.

What can I do in this situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:13:40+00:00Added an answer on June 7, 2026 at 8:13 pm
    class Foo; end
    # => nil
    Object.constants.include?(:Foo)
    # => true
    Object.send(:remove_const, :Foo)
    # => Foo
    Object.constants.include?(:Foo)
    # => false
    Foo
    # NameError: uninitialized constant Foo
    

    EDIT Just noticed your edit, removing the constant is probably not the best way to achieve what you’re looking for. Why not just move one of the Contact classes into a separate namespace.

    EDIT2 You could also rename your class temporarily like this:

    class Foo
      def bar
        'here'
      end
    end
    
    TemporaryFoo = Foo
    Object.send(:remove_const, :Foo)
    # do some stuff
    Foo = TemporaryFoo
    Foo.new.bar #=> "here"
    

    Again, the trouble with this is that you’ll still have the newer Contact class so you’ll have to remove that again. I would really recommend name-spacing your classes instead. This will also help you avoid any loading issues

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can dynamically define a class method for a class like so: class Foo
For a <select> control, we can use the onchange attribute to run some javascript
When I load andand in Ruby 1.9 I get the warning andand.rb:111: warning: undefining
I'd like to remove a class method that gets added to my class via
Let's say I have a Perl class that has a DESTROY method. This method
Is there a way of formatting text in Vim that respects underlined headings? In
I've got a simple Tkinter application I've written, with a few buttons at the
Question for CSS designers. How do I add anchor behavior to any css class
When assigning a default value to a uint parameter in a C# method argument,
I am implementing a spellchecker and wondered if there is an easy/obvious way of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.