Project

General

Profile

Actions

Feature #18841

open

Proposal: autoload_relative

Added by fxn (Xavier Noria) about 2 months ago. Updated about 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:109003]

Description

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) about 2 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) about 2 months ago

Note that this is a duplicate of https://bugs.ruby-lang.org/issues/15330, which includes some real world usage stats.

Making it strict seems like a good idea too.

Updated by fxn (Xavier Noria) about 2 months ago

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

Updated by ioquatix (Samuel Williams) about 2 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) about 2 months ago

Actions

Also available in: Atom PDF