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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:24:33+00:00 2026-05-29T08:24:33+00:00

Usually there are lib, bin and spec(or test) folders in the ruby project(at least,

  • 0

Usually there are lib, bin and spec(or test) folders in the ruby project(at least, in the generated gem). Now there are many require_relative imports in each file in the lib and also ugly require_relative ‘../lib/some_files’ in the spec(or test) file. Is there any way to eliminate all those require_relative imports? I think there’s some kind of trick with FILE but I don’t know which exactly.

  • 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-29T08:24:33+00:00Added an answer on May 29, 2026 at 8:24 am

    You can do something like this:

    current_folder = File.expand_path('../', __FILE__) # get absolute directory
    Dir["#{current_folder}/subfolder/**/*.rb"].each {|f| require f}
    

    This will require all files in the subfolder of current directory. You can use require_relative as well because it is Kernel function, nothing special.

    But if these files depend on each other and must be loaded in the defined order then you will have to manually specify require/require_relative in the defined order.


    Also if you need to eliminate require duplication in spec and lib, then you can go this way: add file with the name of top-level namespace to the root of your /lib folder, require nested files from here and then require this single file from /spec/spec_helper.rb:

    # /lib/coolgem.rb
    module CoolGem; end
    
    current_folder = File.expand_path('../', __FILE__) 
    Dir["#{current_folder}/coolgem/*.rb"].each {|f| require f}
    
    
    # /spec/spec_helper.rb
    require_relative '../lib/coolgem'
    

    This is simplified example and you can have such initialization files for each nested module etc.

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

Sidebar

Related Questions

In methods of controllers, there are usually many checks for doing the right thing.
I know there are many questions on how to ignore directories, and I usually
In visual studio or any other IDE, usually there are two build configurations, Debug
My application server needs to notify users about some events via email. Usually there
Design patterns are usually related to object oriented design. Are there design patterns for
Is there something wrong with this link.exe command line? OpenGL32.lib and Glu32.lib are found
I want to add more than one function to a ViewHelper. Usually there is
In writing latex, usually there is a bibliography file, which sometimes contains _ ,
In a banking or similar application there are usually several roles defined and associated
I usually format my project directory like J-P Boodhoo. a main dir containing solution

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.