![]() ![]() If you want to know what exactly ActiveSupport::Concern doing, please checkout /activesupport/lib/active_support/concern.rb. So you need not to write send(:include, InstanceMethods) and send(:extend, ClassMethods). One more thing: If you define module ClassMethods and module InstanceMethods, it will automatically load to Host class. We wish Host class need not to know module dependencies. Okay,ActiveSupport::Concern is the antidote. So it can not access any methods and variables in Host class. The reason is that we include Foo in Bar module now, so when we are inside Foo’s self.included method, its base parameter becomes Bar module (not Host class anymore). ![]() It looks fine, but there is a fatal error which prevents it running. Include Foo # We include Foo here because Bar depends on Foo For this reason, we rewrite it to: module Bar We wish that we can write dependency relationship in modules instead of Host class, so we can just include the module which we want to use it inside Host class. That’s bad! Why should we need to know modules dependency inside Host class? :/ It means we have to include all dependent modules. # self.included will be executed when Foo is includedīut there is a hateful disadvantage: we have to include both Foo and Bar inside Host class. Therefore we can write code like this: module Foo ![]() And there is a class Host which intends to include Bar features. Suppose we have two modules which have dependency relationship. It makes managing module dependencies management very easy and intuitional.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |