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 7185477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:25:26+00:00 2026-05-28T18:25:26+00:00

So, I need to make a method within a class protected from re-definition. I

  • 0

So, I need to make a method within a class protected from re-definition. I am not really sure how else to explain it so here’s the code:

module Foo
    def bar
        p "lol"
    end
end

Now, that’s the original Foo#bar method and I need it to be like a constant. So, I did come up with a solution. That was to save the method in a Constant and detect when someone tried changing it it would simply re-load it from that constant (it was a Proc object):

module Foo
        Original_bar = Proc.new { p "lol" }

        def bar
            Original_bar.call
        end

        def self.method_added(method_name)
            if method_name == :bar
                def Foo::bar
                    Original_bar.call
                end
            end
        end
end

But this isn’t completely safe since once could use the same "trick" I did to bypass method_added and I am not really fond of this, since it’s not very ruby-like.


Tests

A normal test:

module Foo
    def bar
        p "lmao"
    end
end

Foo::bar # => "lol"

And another test using the trick:

def Foo::bar
    p "rofl"
end

Foo::bar # => "rofl"

tl;dr

How to make a method in Ruby "unredefinable" (if that’s even a word)?

  • 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-05-28T18:25:27+00:00Added an answer on May 28, 2026 at 6:25 pm

    If you freeze the module that should prevent method being added to it.

    Note that a c extension can unfreeze variables (see evil.rb for example.

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

Sidebar

Related Questions

I have method in a class that I need to make sure is only
There’s a really cool diff class hosted by Google here: http://code.google.com/p/google-diff-match-patch/ I’ve used it
I need to make some reflective method calls in Java. Those calls will include
I need to make sure that user can run only one instance of my
Is it possible to inherit a method within a class? Or does it always
Aloha, I have a custom control that I need to instantiate from within a
I have a .Net C# class where I need to make a variable public.
I am writing a simple Interface class as below. I need to make only
I have a nested ListView control and within the ItemTemplate I need to make
I have a crystal report file I need make a tiny edit in. It

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.