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

  • Home
  • SEARCH
  • 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 7515857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:56:49+00:00 2026-05-30T00:56:49+00:00

I am developing a Gem that will allow users to auto-require, instantiate, and register

  • 0

I am developing a Gem that will allow users to auto-require, instantiate, and register classes in a specific directory. I’m just not sure how to achieve this. This is what I’ve come up with so far…

Dir[Dir.pwd + '/extensions/*.rb'].each do |file|
  require file
  extension_class = # instantiate the class here
  MyApp.extensions << extension_class
end

How can I instantiate the class without knowing what it is called?

  • 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-30T00:56:50+00:00Added an answer on May 30, 2026 at 12:56 am

    It sounds like you are making a system for writing extensions to software. Since the extension classes are probably all similar in some way, such as sharing some common methods, it might make sense to have a base class called Extension from which all the extension classes inherit.

    In fact, this is very useful to the stated problem because when a user inherits from your class you can detect it, and add the subclass to a list. Here is some proof of concept code:

    class Extension
      class << self
        attr_reader :list
      end
      @list = []
    
      def self.inherited(klass)
        @list << klass.new
      end
    end
    
    # The MyExt class would be in another file, loaded by require.
    class MyExt < Extension
    end
    
    p Extension.list   # => [#<MyExt:0xb777d884>]
    

    If you don’t want to use subclassing for some reason, it is equally easy to make Extension be a module and use the included hook instead of inherited.

    Personally, I would just remove the call to .new because I don’t see a reason to instantiate the class right away, but that’s up to you. I would just store a list of classes.

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

Sidebar

Related Questions

When developing a desktop application in .NET, is it possible to not require the
I'm currently developing a gem that handles mobile devices. The controller looks like this:
I'm developing a new gem (fork of nifty-generators). Right now my deployment consists on
Developing a website and just trying to get back into the swing of (clever)
I am developing a Ruby gem which is meant to be used as a
Developing rails has become lots of fun over the year that I've done it,
I am developing a Rails app using OmniAuth, OmniAuth-salesforce and this gem: https://github.com/heroku/databasedotcom I
I'm having a problem using ActiveSupport's core extensions on a gem I am developing.
I'm developing a library (a gem) using Ruby 1.9.2. My Gemspec looks like the
I am developing an application which users authlogic for authentication. I would like some

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.