简体   繁体   中英

Ruby namespaced class confusion

Here's the setup:

# app_controller.rb
class AppController; end

# org/app_controller.rb
module Org
  class AppController < ::AppController; end
end

# org/admin/app_controller.rb
module Org
  class Admin::AppController < AppController; end
end

Why does Org::Admin::AppController inherit from AppController , and not Org::AppController , considering that the class definition is namespaced?

This is because by the time you opened Org::Admin::AppController , Org::AppController must not have been defined, but ::AppController must have been . Perhaps your files are not being 'required' in the order you assumed them to be? You might solve this by adding a require <file containing base class> in the file where you create your derived class.

(Minor style guideline: Don't use :: to refer to classes and modules that you are opening for definition.)

Edit reason: I ran some tests and I must have been mistaken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM