【发布时间】:2018-11-02 21:31:53
【问题描述】:
module A
def before
puts :before
end
end
module B
before
end
class Test
include A
include B
end
因此,目标是在解析模块 B 时调用 before,而不在模块 B 中使用 extend A。
Ruby 2.5.1
【问题讨论】:
-
如果您在
module A中定义为self.before并在module B中调用A::before对您来说是否有效? -
“解析模块 B”——此时,它没有机会知道
before应该解析为A。因此,鉴于当前的定义/限制,这是不可能的。 -
您能否详细说明您要达到的目标?也许还有另一种方法可以做到这一点
-
解析期间不能执行代码。 Ruby 没有解析时代码执行,例如Lisp 有。
-
@LeticiaEsperon - 为路由器创建前置过滤器。