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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:44:29+00:00 2026-06-10T03:44:29+00:00

In Ruby I can require all class files in a directory at once: Dir[/path/to/directory/*.rb].each

  • 0

In Ruby I can require all class files in a directory at once:

Dir["/path/to/directory/*.rb"].each {|file| require file }

But what if the classes have inheritance relationships? How to avoid requiring the subclass before superclass? (or it’s impossible?)

  • 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-10T03:44:30+00:00Added an answer on June 10, 2026 at 3:44 am

    Actually there is a dirty hack, suppose you’ve named all your files correspond to class names. But this approach works only if you have just inheritance relationships between different classes. Method overriding multiple times is not taken into account.

    Let’s say you have two rb files in the to be required directory: engineer.rb and human.rb.

    — engineer.rb

    class Engineer < Human
    end
    

    — human.rb

    class Human
    end
    

    And in another file which is gonna require them, if you specify engineer first you’ll get an error:

    require 'engineer' #=> uninitialized constant Human (NameError)
    require 'human'
    

    So we can simply play with the powerful weapon autoload:

    Dir['/path/to/directory/*.rb'].each do |f|
      module_name = /\/([^\/\.]+?)\.rb$/.match(f)[1].capitalize.to_sym
      autoload(module_name, f)
    end
    
    #=> autoload :Engineer, 'engineer'
    #=> autoload :Human, 'human'
    
    puts Engineer.new #=> #<Engineer:0x007f91728847e8>
    

    Basically autoload will require file on demand, thus the sequence of requiring is no more a problem.

    If you want to know more about autoload, please check here:
    http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-autoload
    http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html

    But please be aware that autoload will be dead soon (because it’s not thread safe), please refer to here (by Matz):
    http://www.ruby-forum.com/topic/3036681

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

Sidebar

Related Questions

I am trying to load all of the ruby files in a directory from
In Ruby I can use result << (yield element) and everything works, but if
Is there a gem in Ruby which can write to an Excel file, with
I have been using these files all day... And then suddenly this is now
I have a Date class which I would like to use to overwrite Ruby's
I have test app written on ruby, using Sinatra+Sequel. config.ru: require './main' run Sinatra::Application
I started using NoSQL with Ruby. Here's the code I have require 'mongo' require
Ok, suppose I have Ruby program to read version control log files and do
I am asking this for Ruby, but I guess this principle can be applied
In Ruby I can call methods with array elements used as positional parameters like

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.