我想在模块中放置一些代码,如果未定义某些方法,则会抛出错误。
该模块依赖于该方法的外部定义,因为该方法的实现对于所有类都是不同的。这段代码将帮助开发人员尽早知道他们忘记了实现该方法,而不是当他们尝试使用模块的功能时。
module MyModule
def self.included(klass)
raise "MyModule: please `def my_method` on #{klass}" unless klass.respond_to?(:my_method)
end
end
如果未定义方法,我可以轻松地在模块的包含定义中引发错误,但是由于大多数模块都包含在文件顶部,因此很可能我所需的方法是在类中定义的,但不是在包含我的模块之前定义的。
class MyClass
include MyModule
def self.my_method
# ...
end
end
这仍然会引发错误:(
只有当方法确实没有在类定义中定义时才可能引发错误吗?几乎需要一个 class.onload 回调。如果没有,任何其他关于如何减轻这种可能性的想法程序员可能会在不定义所需方法的情况下包含我们的模块?
I would like to put some code in module that throws an error if certain method is not defined.
This module relies on the external definition of this method, since this method's implementation is different for all classes. This code would help developers know early that they forgot to implement the method rather than when they tried to use features of the module.
module MyModule
def self.included(klass)
raise "MyModule: please `def my_method` on #{klass}" unless klass.respond_to?(:my_method)
end
end
I can easily raise an error in a module's included definition if a method is not defined, however since most modules are included at the top of a file, it's likely that my required method is defined in the class, but not before my module is included.
class MyClass
include MyModule
def self.my_method
# ...
end
end
This would still raise an error :(
Is it possible to raise an error only if the method truly is not defined in the class definition? Almost need a class.onload callback of sorts. If not, any other ideas for how to mitigate the possibilities that a programmer might include our module without defining this needed method?
发布评论
评论(3)
听起来您想使用
method_missing
和define_method
。如果您确实使用
method_missing
,请不要忘记:super
。respond_to?
方法,看看这个问题,加上这个和那个。
更新:
听起来目标是像 Java 或 C++ 那样进行静态方法检查。这在 ruby 中并没有多大意义:-(
因为在 ruby 中:
Foo
不 时拥有一个方法是没有意义的。在类加载 类定义确实是尝试一下:
您将在第一次且仅在第一次使用 Foo 时看到“Hi”,这就是 devise 挂钩来发挥他们的魔力。
所以也许按照私有约定,开发人员会在相关类的底部添加一个
check_class
方法调用?我理解其意图,但它似乎在战斗 作为一个主要使用
Java 的人,我很欣赏这种挫败感:重复将代码推入生产环境却缺少方法? :-P
Update2:
wrt
onload
在 ruby 中,禁止使用冻结的类始终获取定义的新方法。 (或者一个实例可以获得专门为该实例定义的新方法。)因此,检查方法是否不存在只是快照检查,而不是像静态语言那样确定的检查。这是 ruby 自己的停止问题Sounds like you want to make use of
method_missing
anddefine_method
.If you do use
method_missing
don't forget to:super
for unhandled cases.respond_to?
methodlook at this question, plus this and that.
Update:
It sounds the goal is to do static method checking like Java or c++ does. This is not really meaningful in ruby :-(
Since in ruby:
Foo
does not have a method at class load time is meaningless.With regards to "class on load": A class definition is really executed. Try this:
You will see "Hi" the first and only the first time Foo is used. This is how things like devise hook into do their magic.
So maybe by private convention have developers add a
check_class
method call to the bottom of the classes in question?I understand the intent but it seems like fighting the way ruby is designed to work.
As a mostly Java person I appreciate the frustration. Let me guess: repeated cases of code getting pushed to production that had missing methods? :-P
Update2:
wrt
onload
In ruby barring use of frozen a class get new methods defined all the time. ( Or an instance can get new methods defined just for that instance. ) so checking for a method's nonexistence is only a snapshot check and not as definitive a check as a static language brings to the table. This is ruby's very own Halting problem如何使用该名称声明一个方法,这只会引发错误,以确保用户重新定义该方法?
How about declaring a method with that name, which just raises an error, to make sure the user redefines the method?
假设您的程序在启动时需要所有文件并且不使用任何自动加载等,您可以在需要所有内容之后但在程序实际启动之前使用类似以下内容的内容:
但是,我个人总是使用 Dogbert 的解决方案。
Assuming your program requires all files when started and does not use any
autoload
and the like, you could use something like the following right after everything is required, but before the program actually starts:However, I personally always use Dogbert's solution.