Feature #18841
openProposal: autoload_relative
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.
Updated by ioquatix (Samuel Williams) over 2 years 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) over 2 years 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) over 2 years ago
Oh, I should have searched before! Can't close this one myself.
Updated by ioquatix (Samuel Williams) over 2 years 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.
Updated by byroot (Jean Boussier) over 2 years ago
- Related to Feature #15330: autoload_relative added
Updated by rubyFeedback (robert heiler) almost 2 years ago
I am not sure we should add more methods from within that
family (autoload-family). Personally I would even remove
require_relative, but I guess too much code depends on it
nowadays.