Module Baz
def foo
super
:baz
end
end
Class A
prepend Baz
def foo
:bar
end
end
A.new.foo //works fine
现在如果我将我的模块转换为关注模块,它就不是......
module BazConcern
extend ActiveSupport::Concern
included do
def foo
super
:baz
end
end
end
那么我们如何在 ActiveSupport::Concern 中使用 prepend ? 与 ruby 2+
最佳答案
前面添加
ActiveSupport::Concern
(Rails 6.1+)
Rails 6.1 添加了对 prepend
的支持与 ActiveSupport::Concern
.
请参阅以下示例:
module Imposter
extend ActiveSupport::Concern
# Same as `included`, except only run when prepended.
prepended do
end
end
class Person
prepend Imposter
end
还值得一提的是concerning
还更新了:
class Person
concerning :Imposter, prepend: true do
end
end
来源:
关于ruby - 在模块前添加 ActiveSupport::Concern ? ruby 2+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045415/