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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:19:09+00:00 2026-05-31T18:19:09+00:00

I have to add methods to Class in execution time. class ExtendableClass end The

  • 0

I have to add methods to Class in execution time.

class ExtendableClass
end

The methods to add are declared in independent Classes.

module ExtensionClassOne
  def method_one
  end
end

module ExtensionClassTwo
  def method_two
  end
end

I’m looking for an (elegant) mechanism to add all the extension class methods into the ExtendableClass.

Approach 1

I’m thinking in explicily include the extension classes like:

ExtendableClass.send( :include, ExtensionClassOne )
ExtendableClass.send( :include, ExtensionClassTwo )

but it looks a little forced to have to call this private method every time I define a new extension class.

Approach 2

So I was looking for an automatic way to include this methods into my ExtendableClass class.

I’m thinking in declare an specific ancestor for this extension classes:

class ExtensionClassOne < Extension
  def method_one
  end
end

and then I’d need a mechanism to know all the childs of a class… something like the oposite of ancestors.

Once I have this list I can easily ExtendableClass.include all the list of classes. Even if I have to call to the private method here.

Approach 3

Also inheriting from the Extension class and detect in declaration time when this class is used as ancestor. In the way that the ActiveSupport.included method works, like an event binding. Then make the include there.

Any solution for implement approach 2 or approach 3? Do you recommend approach 1? New approachs?

  • 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-31T18:19:10+00:00Added an answer on May 31, 2026 at 6:19 pm

    @fguillen, you are right that the “explicit way is the cleanest approach”. Since that is so, why don’t you use the most “explicit” code which could be imagined:

    class Extendable
    end
    
    class Extendable
      def method_one
        puts "method one"
      end
    end
    
    class Extendable
      def method_two
        puts "method two"
      end
    end
    

    …In other words, if you are defining a module which will be automatically included in a class as soon as it is defined, why bother with the module at all? Just add your “extension” methods directly to the class!

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

Sidebar

Related Questions

I have some ASP.NET page and webservice WebMethod() methods that I'd like to add
Assume I have 10 Methods and 10 Properties. Is there a way to add
I have Excel add-in which I add so many class modules that it is
I have 2 synchronized methods in a class say method1() and method2(). A thread
I have a class called XMLtoXML.java and this is one of it's methods... import
I have a couple extension methods that handle serialization of my classes, and since
I have two classes: Action class, that has a method for executing VBScript files,
I have a COM interface and need to add an accessor method to it.
I have a fairly standard .Net MVC Controller method: public ActionResult Add(Customer cust) {
I am looking to add functionality to a jQuery method. I have the code

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.