Feature #15592

mode where "autoload" behaves like an immediate "require"

Added by akr (Akira Tanaka) about 2 years ago. Updated about 2 years ago.

Target version:


How about a feature to switch "autoload" behavior to "require" immediately.

autoload is a feature for lazy loading.

matz dislikes autoload as [Feature #5653].
I heard that he dislikes class (and other) definitions at arbitrary timing.
I agree that eager loading is safer than lazy loading.

However, lazy loading realize shorter loading time and
it makes development cycle shorter.
It is more important for larger applications as Eregon said in .
It is especially important when library loading causes I/O (code generation from DB schema).

These two, safety of eager loading and easier development of lazy loading, conflicts.
But if we can distinguish production mode and development mode,
we can enjoy both benefits.

So, I propose a feature to select autoload behavior from two modes:

  • autoload behaves as lazy loading as now in development mode
  • autoload behaves as eager loading (immediately invokes "require") in production mode.

There are several idea to switch the mode:

  • $AUTOLOAD_MODE = :eager or :lazy
  • RubyVM.autoload_mode = :eager or :lazy
  • ObjectSpace.autoload_mode = :eager or lazy

I'm not sure there is a good enough one in above list, though.

Related issues

Related to Ruby master - Feature #5653: "I strongly discourage the use of autoload in any standard libraries" (Re: autoload will be dead)Closedmatz (Yukihiro Matsumoto)Actions

Also available in: Atom PDF