我如何检查Ruby对象的方法?

时间:2022-06-01 21:01:55

I am wondering if there is a Ruby method call that shows only the methods defined by the Ruby object it's called from, as opposed to all the methods defined by its ancestor classes, which is what methods seems to do.

我想知道是否有一个Ruby方法调用只显示它调用的Ruby对象定义的方法,而不是它的祖先类定义的所有方法,这是方法似乎做的。

3 个解决方案

#1


32  

methods takes an optional boolean parameter, which specifies whether to also list the methods from the object's class and its superclasses or just the object's singleton methods. So you can do obj.methods(false) to only get the singleton methods defined on obj.

methods采用可选的布尔参数,该参数指定是否还列出来自对象类及其超类的方法,或仅列出对象的单例方法。因此,您可以执行obj.methods(false)以仅获取在obj上定义的单例方法。

If you want the methods defined by the object's class, but not those defined by its superclasses, you can get that by calling instance_methods(false) on the object's class, so it's obj.class.instance_methods(false).

如果你想要对象的类定义的方法,而不是它们的超类定义的方法,你可以通过在对象的类上调用instance_methods(false)来获得它,所以它是obj.class.instance_methods(false)。

#2


4  

I'm partial to obj.methods.sort but some of the other answers are better in certain cases as they describe

我偏爱obj.methods.sort,但在某些情况下,其他一些答案会更好

You can also use obj.methods.sort.grep /foo/ to find method names matching the regexp. This is helpful when you have an idea of what you're looking for.

您还可以使用obj.methods.sort.grep / foo /查找与regexp匹配的方法名称。当您知道自己在寻找什么时,这会很有帮助。

#3


1  

You have a few options - object.methods, object.public_methods, object.singleton_methods... it depends on what you want. Since they both return an array, you might want to try something like:

你有几个选项 - object.methods,object.public_methods,object.singleton_methods ......这取决于你想要什么。由于它们都返回一个数组,你可能想尝试类似的东西:

# obj is the current object
parent = obj.class.superclass

methods = (obj.methods - parent.methods)

#1


32  

methods takes an optional boolean parameter, which specifies whether to also list the methods from the object's class and its superclasses or just the object's singleton methods. So you can do obj.methods(false) to only get the singleton methods defined on obj.

methods采用可选的布尔参数,该参数指定是否还列出来自对象类及其超类的方法,或仅列出对象的单例方法。因此,您可以执行obj.methods(false)以仅获取在obj上定义的单例方法。

If you want the methods defined by the object's class, but not those defined by its superclasses, you can get that by calling instance_methods(false) on the object's class, so it's obj.class.instance_methods(false).

如果你想要对象的类定义的方法,而不是它们的超类定义的方法,你可以通过在对象的类上调用instance_methods(false)来获得它,所以它是obj.class.instance_methods(false)。

#2


4  

I'm partial to obj.methods.sort but some of the other answers are better in certain cases as they describe

我偏爱obj.methods.sort,但在某些情况下,其他一些答案会更好

You can also use obj.methods.sort.grep /foo/ to find method names matching the regexp. This is helpful when you have an idea of what you're looking for.

您还可以使用obj.methods.sort.grep / foo /查找与regexp匹配的方法名称。当您知道自己在寻找什么时,这会很有帮助。

#3


1  

You have a few options - object.methods, object.public_methods, object.singleton_methods... it depends on what you want. Since they both return an array, you might want to try something like:

你有几个选项 - object.methods,object.public_methods,object.singleton_methods ......这取决于你想要什么。由于它们都返回一个数组,你可能想尝试类似的东西:

# obj is the current object
parent = obj.class.superclass

methods = (obj.methods - parent.methods)