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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:20:42+00:00 2026-05-12T00:20:42+00:00

Is there a version of require in ruby that either loads the whole file,

  • 0

Is there a version of require in ruby that either loads the whole file, or nothing at all?

The problem is that require starts loading from the top, and if it faces problems you end up with uncompleted definitions, for example, the following would still load a class A even if module C is not defined:

class B
  include C
end

In my particular case, I have a large set of inter-dependent files, and a loader that loads those files. To exemplify, I will simply the set of files to 4 files (a.rb, b.rb, c.rb and w.rb). The following is a listing of those files:

# In file a.rb
class A
  @foo = []
  @foo.push("in A")

  def self.inherited(subclass)
    foo = @foo.dup
    subclass.instance_eval do
      @foo = foo
    end
  end

  def self.get_foo
    @foo
  end
end

# In file b.rb
class B < A
  include C # if C is not already defined, the following line will not get executed although B will be defined.
  @foo.push("in B")
end

# In file c.rb
module C
end

# In file w.rb
class W < B
  @foo.push("in W")
end

The loader works by getting a list of current files, trying to require them one by one. If any file fails, it remains in the list and is tried again later. The code is something like this: (removed a lot of details for simplicity)

# In file loader.rb
files = Dir["*.rb"].reject { |f| f =~ /loader/ }
files.sort! # just for the purpose of the example, to make them load in an order that causes the problem
files.reject! { |f| require(f) rescue nil } while files.size > 0

I would ultimately want it load A, then find that B can’t be loaded alone (so skip it), then load C, then find W can’t yet be loaded (so skip it), then go back to B then W.

In that case, the output of p W.get_foo would be ["in A", "in B", "in W"], which is what I want.

What actually happens is that it loads A, then partially loads B, then C, then when it comes to W, it believes it can load it (since B is already defined). This triggers the self.inherited code at an incorrect time, and copies a non-ready-yet value of @foo, giving the output of p W.get_foo incorrectly to be ["in A", "in W"].

Having an all-or-nothing require would solve it.

Any ideas?

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

    If one file depends on another, that file should require the dependency itself. For example, b.rb should look like this:

    require 'a'
    require 'c'
    
    class B < A
      include C # if C is not already defined, the following line will not get executed although B will be defined.
      @foo.push("in B")
    end
    

    and w.rb should look like this:

    require 'b'
    
    class W < B
      @foo.push("in W")
    end
    

    Afterwards, the outer load order no longer matters, nor does an “all-or-nothing” require approach. When b is loaded, it will first see the require for a and realize it’s already been loaded, then it will require c because it realizes it hasn’t yet loaded it. When c is required again, it will skip it from the outer loop.

    Note: Be careful about your $LOAD_PATH and the paths passed to require. Ruby only recognizes duplicate requires when the paths are the same. It is best to use relative paths (relative to a path in the $LOAD_PATH) instead of absolute paths; otherwise, a file might get loaded twice.

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

Sidebar

Ask A Question

Stats

  • Questions 159k
  • Answers 159k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Regular expression are not the answer to every single problem.… May 12, 2026 at 11:26 am
  • Editorial Team
    Editorial Team added an answer components shouldn't be in completely different locations - they should… May 12, 2026 at 11:26 am
  • Editorial Team
    Editorial Team added an answer From MSDN: When using the asterisk wildcard character in a… May 12, 2026 at 11:26 am

Related Questions

Ok, suppose I have Ruby program to read version control log files and do
I need to create a photo gallery for a website running IIS 4.0 or
So we have this web app where we support UTF8 data. Hooray UTF8. And
My team is currently trying to automate the deployment of our .Net and PHP

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.