ruby-on-rails - 如何检查method_defined?来自在类中使用的模块,其中包含模块后定义的方法
问题描述
如何定义一个模块来检查使用该模块的类中是否存在实例方法。该模块通常包含在文件的开头,而方法是在之后定义的。我正在使用 Rails。
带钩子的模块
module MyModule
extend ActiveSupport::Concern
included do
raise "Foo" if method_defined? :bar
end
end
以下代码中Foo
永远不会引发错误,我怎样才能让它引发错误?
class MyClass
include MyModule
def bar
puts "Hello from Bar"
end
end
Foo
以下代码中出现错误:
class MyOtherClass
def bar
puts "Hello from Bar"
end
include MyModule
end
解决方案
这是一个纯 Ruby 的答案。我不知道 Rails 是否支持 Ruby 不支持的回调,这在这里有用。
正如@Amadan 所说,该模块不是读心器;要查看在类上定义的实例方法,需要在包含模块之前定义该方法。方法Module#included将包含它的模块作为参数。它需要它,因为self
是MyModule
执行该方法的时间。
module MyModule
def self.included(mod)
puts "self = #{self}"
puts ":bar is defined" if mod.method_defined? :bar
puts ":foo is defined" if mod.method_defined? :foo
puts ":goo is defined" if mod.method_defined? :goo
end
def goo
end
end
class MyClass
def bar
end
include MyModule
end
印刷
self = MyModule
:bar is defined
:goo is defined
请注意,在( ) 中Module#included
定义的实例方法包含在.MyModule
goo
MyClass
推荐阅读
- reactjs - 如何将reducer的返回值设置为react js组件中的状态对象
- amazon-web-services - 当 2 个 lambda 具有相同的运动流时,会有什么行为?
- python - 使用 Paramiko 从远程服务器执行命令到另一个远程服务器
- python - 错误:PyAudio-0.2.11-cp38-cp38-win_amd64.whl 不是此平台支持的轮子
- html - 在 CSS 中移动侧边栏
- python - RGB 到 YIQ 并返回 python
- javascript - 如何在函数中创建模态页面?
- snappy - Laravel Snappy:多余的页面
- gdb - GDB“查找”命令提前终止
- c - 处理 C 中的双重加法/减法错误