Ruby - 确定方法起源? [英] Ruby - determining method origins?

查看:30
本文介绍了Ruby - 确定方法起源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当发送消息时,Ruby 对象会搜索以查看它是否具有使用该名称的方法来响应.它的方法查找按以下顺序搜索,并使用它找到的第一个方法.

When sent a message, a Ruby object searches to see whether it has a method by that name to respond with. Its method lookup searches in the following order, and uses the first method it finds.

  1. 在自身上定义的单例方法(也称为特征类"上的方法)
  2. 在其类中定义的方法
  3. 以包含的相反顺序混入其类的任何模块(只有给定模块的最早包含有任何影响 - 如果超类包含模块 A,并且子类再次包含它,则它在子类中被忽略;如果子类包括 A 然后 B 然后 A,第二个 A 被忽略)(更新:注意这是在 Module.prepend 存在之前编写的)
  4. 它的父类
  5. 混合到父类、父类的父类等中的任何方法
  1. Singleton methods defined on itself (aka methods on its "eigenclass")
  2. Methods defined in its class
  3. Any modules mixed into its class, in reverse order of inclusion (only the earliest inclusion of a given module has any effect - if the superclass includes module A, and the subclass includes it again, it’s ignored in the subclass; if the subclass includes A then B then A, the second A is ignored) (update: note that this was written before Module.prepend existed)
  4. Its parent class
  5. Any methods mixed into the parent class, the parent's parent, etc.

或者,更简单地说,它查看自身,然后按照列出的顺序查看 self.class.ancestors 中的所有内容.

Or, to put it more simply, it looks on itself, then everything in self.class.ancestors in the order they're listed.

在调用方法时遵循此查找路径;如果您创建一个类的实例,然后重新打开该类并添加一个方法或通过模块混合一个方法,则现有实例将获得对该方法的访问权限.

This lookup path is followed at the moment the method is called; if you make an instance of a class, then reopen the class and add a method or mix one in via a module, the existing instance will gain access to that method.

如果这一切都失败了,它会查看它是否有 method_missing 方法,或者它的类是否有,它的父类等等.

If all of this fails, it looks to see if it has a method_missing method, or if its class does, its parent class, etc.

我的问题是:除了手动检查代码,或者使用像 puts "I'm on module A!" 这样的示例方法,你能知道给定的方法来自哪里吗? 例如,您能否列出一个对象的方法并看到这个在父类上,这个在模块 A 上,这个在类上并覆盖父类"等等?

My question is this: aside from examining the code by hand, or using example methods like puts "I'm on module A!", can you tell where a given method came from? Can you, for example, list an object's methods and see "this one is on the parent class, this one is on module A, this one is on the class and overrides the parent class," etc?

推荐答案

Object#method 返回一个 Method 对象提供有关给定方法的元数据.例如:

Object#method returns a Method object giving meta-data about a given method. For example:

> [].method(:length).inspect
=> "#<Method: Array#length>"
> [].method(:max).inspect
=> "#<Method: Array(Enumerable)#max>"

在 Ruby 1.8.7 及更高版本中,您可以使用 Method#owner 以确定定义该方法的类或模块.

In Ruby 1.8.7 and later, you can use Method#owner to determine the class or module that defined the method.

要获取所有方法的列表以及定义它们的类或模块的名称,您可以执行以下操作:

To get a list of all the methods with the name of the class or module where they are defined you could do something like the following:

obj.methods.collect {|m| "#{m} defined by #{obj.method(m).owner}"}

这篇关于Ruby - 确定方法起源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆