【发布时间】:2019-12-12 18:00:11
【问题描述】:
例如,我有一些具有此初始化的类:
class SomeClass < ActiveRecord
call_some_method('bla_bla_bla')
def some_method
return 1
end
# end of initialization or definition(not really sure, sorry)
# and here I want to call some code which can be added in other place of project
end
我想用我自己的代码添加钩子,它可以在初始化后添加或调用类的方法。我没有能力将一些代码直接添加到类定义中,这里我的意思不是关于类实例的初始化。
有没有办法做到这一点?
【问题讨论】:
-
请使用真正有意义的代码。从
ActiveRecord继承是不可能的,因为它不是一个类。你的意思是ActiveRecord::Base?做到这一点的唯一方法是使用跟踪。 Is there a hook similar to Class#inherited that's triggered only after a Ruby class definition? 的可能重复项
标签: ruby-on-rails ruby