From 9f7222b1ed58afec13829dd12698549f9aae77ad Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 28 Feb 2022 17:01:23 +0100 Subject: [PATCH] Modernize ActiveSupport::Autoload We no longer need to define instance variables to avoid uninitialized instance variable warnings. Also rather than to keep a hash of `constant -> path` forever, we can simply keep a list of constants and call `const_get` for each of them. And finally we can clear the list we kept, as it's just wasted memory. --- .../active_support/dependencies/autoload.rb | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/activesupport/lib/active_support/dependencies/autoload.rb b/activesupport/lib/active_support/dependencies/autoload.rb index 1cee85d98f..95f3e0b028 100644 --- a/activesupport/lib/active_support/dependencies/autoload.rb +++ b/activesupport/lib/active_support/dependencies/autoload.rb @@ -26,11 +26,13 @@ module ActiveSupport # MyLib.eager_load! module Autoload def self.extended(base) # :nodoc: - base.class_eval do - @_autoloads = {} - @_under_path = nil - @_at_path = nil - @_eager_autoload = false + if RUBY_VERSION < "3" + base.class_eval do + @_autoloads = nil + @_under_path = nil + @_at_path = nil + @_eager_autoload = false + end end end @@ -41,7 +43,8 @@ module ActiveSupport end if @_eager_autoload - @_autoloads[const_name] = path + @_eagerloaded_constants ||= [] + @_eagerloaded_constants << const_name end super const_name, path @@ -69,11 +72,10 @@ module ActiveSupport end def eager_load! - @_autoloads.each_value { |file| require file } - end - - def autoloads - @_autoloads + if @_eagerloaded_constants + @_eagerloaded_constants.each { |const_name| const_get(const_name) } + @_eagerloaded_constants = nil + end end end end