3
我不确定这是这个问题的正确标题,但我不知道还有什么要问的。我有需要全局注册的类,以便稍后调用它们。除了非常重要的部分,我大部分工作。当孩子继承父类时,它会注册一个新实例,但是当调用类方法时,我无法弄清楚如何设置我需要的实例变量。Ruby:如何从类方法设置实例变量?
class MyExtension < ExtensionBase
on_message '/join (.+)' do |username|
# this will be a callback function used later
end
end
class ExtensionBase
def self.inherited(child)
MainAppModule.registered_extensions << child.new
end
def self.on_message(string, &block)
# these need to be set on child instance
@regex = Regexp.new(string)
@on_message_callback = block
end
def exec(message)
args = @regex.match(message).captures
@on_message_callback.call(args)
end
end
# somewhere else in the code, I find the class that I need...
MainAppModule.registered_extensions.each do |child|
puts child.regex.inspect # this is nil and I dont want it to be
if message =~ child.regex
return child.exec(message)
end
end
我该如何设计这样才能设置@regex
以便我可以在循环内访问它?