Ruby:如果未找到类方法,则在模块中引发错误

发布于 2024-11-27 03:56:21 字数 605 浏览 1 评论 0 原文

我想在模块中放置一些代码,如果未定义某些方法,则会抛出错误。 该模块依赖于该方法的外部定义,因为该方法的实现对于所有类都是不同的。这段代码将帮助开发人员尽早知道他们忘记了实现该方法,而不是当他们尝试使用模块的功能时。

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

默嘫て 2024-12-04 03:56:21

听起来您想使用 method_missingdefine_method

如果您确实使用 method_missing,请不要忘记:

  • 针对未处理的情况调用 super
  • 还实现了 respond_to? 方法,

看看这个问题,加上这个那个

更新:

听起来目标是像 Java 或 C++ 那样进行静态方法检查。这在 ruby​​ 中并没有多大意义:-(

因为在 ruby​​ 中:

  • 对象的每个实例都有自己的特征类。给定的对象可能在运行时混合了必要的方法。所以仅仅因为 Foo 不 时拥有一个方法是没有意义的。
  • 像 RoR 这样的框架会挂钩 method_missing 并动态创建数据库查询方法所需的方法,因此该方法在需要时可能存在(或不存在):

在类加载 类定义确实是尝试一下:

class Foo 
  p "Hi"
end

您将在第一次且仅在第一次使用 Foo 时看到“Hi”,这就是 devise 挂钩来发挥他们的魔力。

class User < ActiveRecord::Base
  # **CALL 'devise' method**
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  # **CALL attr_accessible method**
  attr_accessible :email, :password, :password_confirmation
end

所以也许按照私有约定,开发人员会在相关类的底部添加一个 check_class 方法调用?

我理解其意图,但它似乎在战斗 作为一个主要使用

Java 的人,我很欣赏这种挫败感:重复将代码推入生产环境却缺少方法? :-P

Update2:

wrt onload 在 ruby​​ 中,禁止使用冻结的类始终获取定义的新方法。 (或者一个实例可以获得专门为该实例定义的新方法。)因此,检查方法是否不存在只是快照检查,而不是像静态语言那样确定的检查。这是 ruby​​ 自己的停止问题

Sounds like you want to make use of method_missing and define_method.

If you do use method_missing don't forget to:

  • call super for unhandled cases.
  • also implement a respond_to? method

look 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:

  • Each instance of an object has its own eigenclass. A given object may have the necessary methods mixed in at runtime. So just because Foo does not have a method at class load time is meaningless.
  • Frameworks like RoR hooks method_missing and dynamically create methods needed for the database query methods, so the method may exist (or not) when it is needed.

With regards to "class on load": A class definition is really executed. Try this:

class Foo 
  p "Hi"
end

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.

class User < ActiveRecord::Base
  # **CALL 'devise' method**
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  # **CALL attr_accessible method**
  attr_accessible :email, :password, :password_confirmation
end

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

罪歌 2024-12-04 03:56:21

如何使用该名称声明一个方法,这只会引发错误,以确保用户重新定义该方法?

module MyModule
  def my_method
    raise "Please implement me"
  end
end


class MyClass 
  include MyModule
  def my_method
    # do something
  end
end

How about declaring a method with that name, which just raises an error, to make sure the user redefines the method?

module MyModule
  def my_method
    raise "Please implement me"
  end
end


class MyClass 
  include MyModule
  def my_method
    # do something
  end
end
故事灯 2024-12-04 03:56:21

假设您的程序在启动时需要所有文件并且不使用任何自动加载等,您可以在需要所有内容之后但在程序实际启动之前使用类似以下内容的内容:

classes_to_check = Object.constants.find_all do |const|
  klass = Object.const_get(c)
  klass.ancestors.include?(MyModule) if klass.kind_of?(Module)
end

classes_to_check.each do |klass|
  raise "MyModule: please `def my_method` on #{klass}" \
    unless klass.respond_to?(:my_method)
end

但是,我个人总是使用 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:

classes_to_check = Object.constants.find_all do |const|
  klass = Object.const_get(c)
  klass.ancestors.include?(MyModule) if klass.kind_of?(Module)
end

classes_to_check.each do |klass|
  raise "MyModule: please `def my_method` on #{klass}" \
    unless klass.respond_to?(:my_method)
end

However, I personally always use Dogbert's solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文