我想在 Ruby 中编写一个基类,它允许扩展它的类注册回调,就像 ApplicationController 对 before_filter 所做的那样:
class AController < ApplicationController
before_filter :foo
def foo
end
end
我想自己写一些类似于 before_filter 的另一面。
class AClass < MyBase
register_callback :callback1
def callback1
puts "called!"
end
def test
call_me_maybe 5
end
end
该call_me_maybe
方法在MyBase
类中定义,可能会调用之前注册的回调。MyBase 的实现是什么样的。