Feature #18841


Proposal: autoload_relative

Added by fxn (Xavier Noria) 7 months ago. Updated 7 months ago.

Target version:


In my experience, autoloads often reflect an existing hierarchical structure.

If a project does not use Zeitwerk, and the user declares autoloads for a class or module, chances are they are for child constants. As an example, see the ActiveRecord module. (Those ones do not have a second argument because we define wrapper that derives it by convention, here).

I think it would be convenient to have an autoload_relative in the line of Kernel#require_relative. It would make existing patterns more concise, and as a practical consequence, you skip $LOAD_PATH lookups too.

Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #15330: autoload_relativeOpenmatz (Yukihiro Matsumoto)Actions

Updated by ioquatix (Samuel Williams) 7 months ago

Yes, great idea, and I can confirm I've run into issues with autoload loading the wrong files if you combine autoload with require_relative.

Also, since this does not exist yet, we can be more strict on how this works, as in autoload_relative can raise an exception if the constant is not defined correctly.

Updated by marcandre (Marc-Andre Lafortune) 7 months ago

Note that this is a duplicate of, which includes some real world usage stats.

Making it strict seems like a good idea too.

Updated by fxn (Xavier Noria) 7 months ago

Oh, I should have searched before! Can't close this one myself.

Updated by ioquatix (Samuel Williams) 7 months ago

Great minds think alike! Thanks @marcandre (Marc-Andre Lafortune).

@fxn (Xavier Noria) I think the next step is to make a PR and then get Matz to review it.

Actions #5

Updated by byroot (Jean Boussier) 7 months ago


Also available in: Atom PDF