假设我们有 A、B、C 类。
A
def self.inherited(sub)
# meta programming goes here
# take class that has just inherited class A
# and for foo classes inject prepare_foo() as
# first line of method then run rest of the code
end
def prepare_foo
# => prepare_foo() needed here
# some code
end
end
B < A
def foo
# some code
end
end
C < A
def foo
# => prepare_foo() needed here
# some code
end
end
如您所见,我正在尝试foo_prepare()
对每个foo()
方法注入调用。
怎么可能呢?
此外,我一直在考虑以我会运行的方式覆盖send
类,而不是让(super) 来执行其余的方法。class A
foo_prepare
send
你们怎么看,解决这个问题的最佳方法是什么?