moduleをmixinするときに、同じ継承ツリーに任意のmoduleをさらにぶら下げる.md

Home   »   moduleをmixinするときに、同じ継承ツリーに任意のmoduleをさらにぶら下げる.md

```ruby
module Core
  def hoge
    2
  end
end

module Wrapper
  def self.extended(base) #:nodoc:
    base.class_eval do
      include Core
    end
  end
end

class App
  extend Wrapper
end

module Wrapper2
  extend Core
end

class App2
  extend Wrapper2
end
```

```ruby
[38] pry(main)> Wrapper2.ancestors
=> [Wrapper2, Core]

[39] pry(main)> Wrapper.ancestors
=> [Wrapper]

[40] pry(main)> App.ancestors
=> [App,
 Core,
 Wrapper,
 ActiveSupport::Dependencies::ZeitwerkIntegration::RequireDependency,
 ActiveSupport::ForkTracker::CoreExtPrivate,
 ActiveSupport::ForkTracker::CoreExt,
 ActiveSupport::ToJsonWithActiveSupportEncoder,
 Object,
 PP::ObjectMixin,
 JSON::Ext::Generator::GeneratorMethods::Object,
 ActiveSupport::Dependencies::Loadable,
 ActiveSupport::Tryable,
 ActiveSupport::ForkTracker::CoreExtPrivate,
 ActiveSupport::ForkTracker::CoreExt,
 Kernel,
 BasicObject]

[41] pry(main)> App2.ancestors
=> [App2,
 Wrapper,
 ActiveSupport::Dependencies::ZeitwerkIntegration::RequireDependency,
 ActiveSupport::ForkTracker::CoreExtPrivate,
 ActiveSupport::ForkTracker::CoreExt,
 ActiveSupport::ToJsonWithActiveSupportEncoder,
 Object,
 PP::ObjectMixin,
 JSON::Ext::Generator::GeneratorMethods::Object,
 ActiveSupport::Dependencies::Loadable,
 ActiveSupport::Tryable,
 ActiveSupport::ForkTracker::CoreExtPrivate,
 ActiveSupport::ForkTracker::CoreExt,
 Kernel,
 BasicObject]
```

Leave a Reply

Your email address will not be published. Required fields are marked *