如何在ruby中显示没有祖先方法的类的所有方法?

时间:2022-10-30 07:35:56

To show all methods defined by a particular class, but without methods that are defined in ancestors classes, I'm writing like this.

为了显示由特定类定义的所有方法,但是没有在祖先类中定义的方法,我这样编写。

foo.methods - foo.class.superclass.methods

Is there better way to do it?

有更好的办法吗?

1 个解决方案

#1


5  

You can get instance methods with the following:

你可以获得实例方法如下:

foo.class.instance_methods(false)

as documented in http://ruby-doc.org/core-1.9.3/Module.html#method-i-instance_methods.

在http://ruby-doc.org/core-1.9.3/Module.html method-i-instance_methods记录。

The documentation for the parameter uses the term "superclasses" in describing what is included if the parameter is truthy, but based on my testing I believe all ancestor-provided methods are excluded/included based on this value, not just those from superclasses.

参数的文档使用术语“超类”来描述如果参数是真实的,将包含什么,但是根据我的测试,我认为所有祖先提供的方法都基于这个值被排除/包含,而不仅仅是超类的方法。

#1


5  

You can get instance methods with the following:

你可以获得实例方法如下:

foo.class.instance_methods(false)

as documented in http://ruby-doc.org/core-1.9.3/Module.html#method-i-instance_methods.

在http://ruby-doc.org/core-1.9.3/Module.html method-i-instance_methods记录。

The documentation for the parameter uses the term "superclasses" in describing what is included if the parameter is truthy, but based on my testing I believe all ancestor-provided methods are excluded/included based on this value, not just those from superclasses.

参数的文档使用术语“超类”来描述如果参数是真实的,将包含什么,但是根据我的测试,我认为所有祖先提供的方法都基于这个值被排除/包含,而不仅仅是超类的方法。