gpt4 book ai didi

ruby - ActiveSupport::Concern 为什么/如何更改祖先查找顺序?

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:09 24 4
gpt4 key购买 nike

考虑以下代码:

require 'active_support/concern'

module Inner
end

module Outer
extend ActiveSupport::Concern
included do
include Inner
end
end

class FirstClass
include Outer
end

module SecondInner
end

module SecondOuter
include SecondInner
end

class SecondClass
include SecondOuter
end

为什么通过 AS::Concern 与普通的 Ruby 包含的模块的祖先顺序不同?

FirstClass.ancestors
# => [FirstClass, Inner, Outer, Object, PP::ObjectMixin, Kernel, BasicObject]

SecondClass.ancestors
# => [SecondClass, SecondOuter, SecondInner, Object, PP::ObjectMixin, Kernel, BasicObject]

最佳答案

ActiveSupport::Concern 不会更改祖先查找顺序。

如果您将 module Outer 更改为使用纯 Ruby 来执行相同的操作,那么 AS 所做的但没有 AS,您会看到,它具有相同的祖先链:

module Outer
def self.included(base)
base.send(:include, Inner)
end
end

SecondClass.ancestors
#=> [SecondClass, SecondOuter, SecondInner, Object, PP::ObjectMixin, Kernel, BasicObject]

关于ruby - ActiveSupport::Concern 为什么/如何更改祖先查找顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40061982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com