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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:09:16+00:00 2026-05-23T21:09:16+00:00

I’m trying to set up autoloading for my classes in a new project of

  • 0

I’m trying to set up autoloading for my classes in a new project of mine using Module#autoload. It’s almost working – the issue is that on first using a constant to be autoloaded, it errors with “uninitialized constant”, but on second usage the constant works as expected.

Code illustrating problem:

init.rb:

# Load lib, other directories will be autoloaded
APPLICATION_ROOT=File.expand_path(File.dirname(__FILE__))
$:.unshift(APPLICATION_ROOT)
Dir.glob("#{APPLICATION_ROOT}/patches/*").each {|p| require p}
Dir.glob("#{APPLICATION_ROOT}/lib/*").each {|p| require p}

# Test autoloading
include Autoload
begin
  puts Sprite.new.inspect
rescue
  puts "Caught an error"
end
puts Sprite.new.inspect # will not error

patches/string.rb:

class String
  def camelize
    self.split("_").map{|word| word.capitalize}.join
  end
end

lib/autoload.rb:

module Autoload
  Dir.glob("#{APPLICATION_ROOT}/app/*/*").each do |path|
    classname = File.basename(path).gsub(/.rb$/,'').camelize
    autoload classname.to_sym, path
  end
end

app/models/sprite.rb:

puts "Sprite Required!"
class Sprite
  puts "Sprite Defining!"
  def initialize
    puts "Sprite Initialized!"
  end
end
puts "Sprite Defined!"

The output:

Sprite Required!
Sprite Defining!
Sprite Defined!
Caught an error
Sprite Initialized!
#<Sprite:0x000000024ee920>

How can I obtain my desired behavior (no initial error)?

  • 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-23T21:09:17+00:00Added an answer on May 23, 2026 at 9:09 pm

    The problem is you are calling autoload in the scope of the module Autoload. In a case like that what ruby is expecting or creating is an autoload for the symbol Autoload::Sprite, when what you want is just Sprite.

    Fortunately the fix is simple:

    module Autoload
      def self.included(mod)
        # ...
        # Call autoload on the scope of the includer
        mod.autoload ...
      end
    end
    

    Or you could also explicitly call autoload on Object, since that’s where your “target scope” for the autoloaded classes most likely is:

    Object.autoload ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.