如何检查Ruby对象的方法? [英] How do I inspect the methods of a Ruby object?

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

问题描述

我想知道是否有一个Ruby方法调用,它仅显示由其调用的Ruby对象定义的方法,而不是其祖先类定义的所有方法,而methods似乎是这样做的. /p>

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.

推荐答案

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

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.

如果您想要由对象的类定义的方法,而不是由其超类定义的方法,则可以通过在对象的类上调用instance_methods(false)来获得,即为obj.class.instance_methods(false).

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).

这篇关于如何检查Ruby对象的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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