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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:00:04+00:00 2026-05-26T23:00:04+00:00

today i was facing a strange problem: got a ‘missing method’ error on a

  • 0

today i was facing a strange problem:
got a ‘missing method’ error on a module, but the method was there and the file where the module was defined was required. After some searching i found a circular dependency, where 2 files required each other, and now i assume ruby silently aborts circular requires.


Edit Begin: Example

File ‘a.rb’:

require './b.rb'

module A
    def self.do_something
        puts 'doing..'
    end
end

File ‘b.rb’:

require './a.rb'

module B
    def self.calling
        ::A.do_something
    end
end

B.calling

Executing b.rb gives b.rb:5:in 'calling': uninitialized constant A (NameError). The requires have to be there for both files as they are intended to be run on their own from command line (i ommitted that code to keep it short).
So the B.calling has to be there. One possible solution is to wrap the requires in if __FILE__ == $0, but that does not seem the right way to go.

Edit End


to avoid these hard-to-find errors (wouldn’t it be nicer if the require threw an exception, by the way?), are there some guidelines/rules on how to structure a project and where to require what? For example, if i have

module MainModule
  module SubModule
    module SubSubModule
    end
  end
end

where should i require the submodules? all in the main, or only the sub in the main and the subsub in the sub?

any help would be very nice.

Summary

An explanation why this happens is discussed in forforfs answer and comments.

So far best practice (as pointed out or hinted to by lain) seems to be the following (please correct me if i’m wrong):

  1. put every module or class in the top namespace in a file named after the module/class. in my example this would be 1 file named ‘main_module.rb.’
    if there are submodules or subclasses, create a directory named after the module/class (in my example a directory ‘main_module’, and put the files for the subclasses/submodules in there (in the example 1 file named ‘sub_module.rb’). repeat this for every level of your namespace.
  2. require step-by-step (in the example, the MainModule would require the SubModule, and the Submodule would require the SubSubModule)
  3. separate ‘running’ code from ‘defining’ code. in the running code require once your top-level module/class, so because of 2. all your library functionality should now be available, and you can run any defined methods.

thanks to everyone who answered/commented, it helped me a lot!

  • 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-26T23:00:05+00:00Added an answer on May 26, 2026 at 11:00 pm

    After asking about this on the Ruby mailing list a while back, when I used to have a file in my libraries just for requiring things, I changed to these two rules:

    1. If a file needs code from another in the same library, I use require_relative in the file that needs the code.

    2. If a file needs code from a different library, I use require in the file that needs the code.

    As far as I understand it, Ruby requires in the order it is asked to, and so it doesn’t matter about circular dependencies.

    (Ruby v1.9.2)

    In answer to the comment about the example showing circular dependency problems:

    actually, the problem with the example isn’t that the requires are circular, but that B.calling is called before the requires have completed. If you remove the B.calling from b.rb it works fine. For example, in irb without B.calling in the code file but run afterwards:

    $ irb
    require ‘/Volumes/RubyProjects/Test/stackoverflow8057625/b.rb’
    => true
    B.calling
    doing..
    => nil

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

Sidebar

Related Questions

I've used jQuery for some months with good results, but today I'm facing something
today the default route for my site stopped working, but the strange thing is
Today, I ran into this weird problem with a user using Mac OS X.
Today I stumbled about a Problem which seems to be a bug in the
Since today I am facing a tricky issue with Google Chrome that I've just
Today I meet a similar problem and it reminds me about this thread. In
Today I use SQLite and send a database-file with the project. However I want
today i have a simple an understable problem for me . Next code: alphavar
today i have direct a problem :P i just need some ideas... how do
I am facing a problem using mod_rewrite. This is what I am using Options

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.