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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:31:48+00:00 2026-06-12T05:31:48+00:00

I have a class which I want other developers to be able to create

  • 0

I have a class which I want other developers to be able to create modules to extend my class and give it more instance variables.

How can I automatically include modules in my class based on files in a particular folder? i.e. load all files and include them as modules for my class

How can I do this and somehow avoid method naming collisions?
I was thinking I could pass the name of the plugin in the initialize method, and do this:

class MyClass
  def initialize(plugin_name=nil)
  end

  def method_missing(method_name)
    "#{plugin_name}".send(method_name)
  end
end

Would something like this work in theory, please help out in the method_missing as I have never written one before. I’m trying to basically specify a particular plugin name to avoid any name collisions.

Or should I, when including the plugins, output an error if there is a method collision?

  • 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-12T05:31:49+00:00Added an answer on June 12, 2026 at 5:31 am

    Allowing external code to be melted in your class sounds dangerous and drives into names collisions as you guessed. It cans also drive into unexpected behavior, since you allow external code to mess with your class internals.

    The best way to handle a plugin would be to encaspulate the behavior that would be replaced.
    Let’s take an example :

    Given you’re building a Logger, we can imagine that you can create plugins to specify where the logs will be wrote.

    You can then create the following class :

    class Logger 
       def initialize(store)
         @store = store 
       end
    
       def info(message)
         @store.write(:info, message) 
       end 
     end 
    
    class StandardOutputStore
      def write(level, message) 
        puts "#{level}: #{message}" 
      end 
    end 
    
    class FileStore
      def initalize(filename)
        @filename = filename
      end
    
      def write(level, message)
        File.open(@filename, "a") do |file|
          file.write("#{level}: #{message}") 
        end
      end
    end
    

    Then you can you instanciate the right plugin this way :

    logger = Logger.new(StandardOutputStore.new)
    logger.warn "hello"
    
    logger = Logger.new(FileStore.new("/tmp/log"))
    logger.warn "hello" 
    

    This way of doing things provides modularity, more flexibility and is more solid than overloading things in your class with external code.

    Loading modules as they are found in a directory is a matter of toying with Dir.glob and require. To explore what had been found under your plugin directory, you can force your users to write plugins in a module, like Foo::Plugins::FileStore and inspect which constants are present in the Foo::Plugins module after requiring the files. See http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-constants for more informations.

    Edit: As you don’t have any interface to map to since you provides the methods to an ERB template, you could do the following :

    Plugins are in the following form :

    module Plugins
      module Emoticons
        def smile
            ":-)"
        end
      end
    end
    

    You can then load them with the following :

    Dir["/plugins/*.rb"].each {|file| require file }
    

    And include them, given TemplateMethods is a module included in the context of your ERB template.

     Plugins.constants.each do |constant|
       mod = Plugins.const_get constant
       TemplateMethods.send(:include, mod)
     end
    

    Using send isn’t very elegant, I’d suggest building a class method on TemplateMethods that encapsulate the whole thing, like TemplateMethods.load_plugins_method! The bang here would warn about the intrusive action done by the dynamic modules inclusion.

    That should do the job 🙂

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

Sidebar

Related Questions

I have a static class in a shared project, which I want to extend
I am create a Class which have a UIWebView property. I want to set
I have a base class, which includes all other files. I can access this
I have a class which I want to add a property with using formula
I have this class which I want to pass around Windows as LPARAM parameter.
I have a custom class which I want to load inside the firstViewController and
So I have this class which extends an activity. But I want to draw
I have a class Test which is in lib folder and I want to
I want to have a base class which dictates the alignment of the objects
I have a class called Trial which has_many results. Now What I want to

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.