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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:42:19+00:00 2026-05-28T16:42:19+00:00

I love the autoload functionality of Ruby ; however, it’s going away in future

  • 0

I love the autoload functionality of Ruby; however, it’s going away in future versions of Ruby since it was never thread-safe.

So right now I would like to pretend it’s already gone and write my code without it, by implementing the lazy-loading mechanism myself. I’d like to implement it in the simplest way possible (I don’t care about thread-safety right now). Ruby should allow us to do this.

Let’s start by augmenting a class’ const_missing:

class Dummy
  def self.const_missing(const)
    puts "const_missing(#{const.inspect})"
    super(const)
  end
end

Ruby will call this special method when we try to reference a constant under “Dummy” that’s missing, for instance if we try to reference “Dummy::Hello”, it will call const_missing with the Symbol :Hello. This is exactly what we need, so let’s take it further:

class Dummy
  def self.const_missing(const)
    if :OAuth == const
      require 'dummy/oauth'
      const_get(const)      # warning: possible endless loop!
    else
      super(const)
    end
  end
end

Now if we reference “Dummy::OAuth”, it will require the “dummy/oauth.rb” file which is expected to define the “Dummy::OAuth” constant. There’s a possibility of an endless loop when we call const_get (since it can call const_missing internally), but guarding against that is outside the scope of this question.

The big problem is, this whole solution breaks down if there exists a module named “OAuth” in the top-level namespace. Referencing “Dummy::OAuth” will skip its const_missing and just return the “OAuth” from the top-level. Most Ruby implementations will also make a warning about this:

warning: toplevel constant OAuth referenced by Dummy::OAuth

This was reported as a problem way back in 2003 but I couldn’t find evidence that the Ruby core team was ever concerned about this. Today, most popular Ruby implementations carry the same behavior.

The problem is that const_missing is silently skipped in favor of a constant in the top-level namespace. This wouldn’t happen if “Dummy::OAuth” was declared with Ruby’s autoload functionality. Any ideas how to work around this?

  • 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-28T16:42:20+00:00Added an answer on May 28, 2026 at 4:42 pm

    This was raised in a Rails ticket some time ago and when I investigated it there appeared to be no way round it. The problem is that Ruby will search the ancestors before calling const_missing and since all classes have Object as an ancestor then any top-level constants will always be found. If you can restrict yourself to only using modules for namespacing then it will work since they do not have Object as an ancestor, e.g:

    >> class A; end
    >> class B; end
    >> B::A
    (irb):3: warning: toplevel constant A referenced by B::A
    
    >> B.ancestors
    => [B, Object, Kernel, BasicObject]
    
    >> module C; end
    >> module D; end
    >> D::C
    NameError: uninitialized constant D::C
    
    >> D.ancestors
    => [D]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I love the Ruby RSpec BDD development style. Are there any good tools for
I love jQuery. I am probably going to have some XML parsing and manipulation
I love using OneNote, however I want more control over the locations of my
I love making named scopes for rails. however, I ran into somewhat of a
I love the idea of Qt, however I use it not only for open
I love RGoogleDocs and use it a lot. However, I don't like entering my
I love Ruby blocks! The idea behind them is just very very neat and
I love being able to write quick and dirty query strings right into the
We love jQuery validate, and really want to keep using it. However, one of
I love Heroku but I would prefer to develop in Scala rather than Ruby

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.