Why initialize instance variables from self.extended to base.class_eval?

Asked 2 years ago, Updated 2 years ago, 40 views

If you read the Rails source, you will see

In ActiveSupport Autoload, click

The following code was found:

def self.extended(base)#:nodoc:
      base.class_evaldo
        @_autoloads={}
        @_under_path=nil
        @_at_path=nil
        @_eager_autoload=false
      end
end

for the class when extended

I'm defining class instance variables.

Why do I not use class variables?

Unlike class instance variables,
For class variables,
because the inheritance destination can reference the parent variable. I think it will work the same way as the above code.

ruby-on-rails ruby

2022-09-30 17:49

1 Answers

Self-resolved.

When extending, class variables cannot be referenced in the inheritance destination, so
Failed to define class instance variable for inheritance destination in self.extended.


2022-09-30 17:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.