[英]How to get a list of methods defined in a subclass in Ruby?
Let's say I have some ruby code like this: 假设我有一些像这样的ruby代码:
class ParentClass
def method1
# some code
end
end
class MyAwesomeSubclass < ParentClass
def method1
# some code
end
def method2
# more code
end
end
Is there a way to get a list of method defined on the subclass, so [:method1, :method2]
in this example? 有没有一种方法可以获取在子类上定义的方法列表,因此在此示例中为[:method1, :method2]
? My first impulse was to say MyAwesomeSubclass.methods - ParentClass.methods
, but this would remove :method1
since method1 is defined on the parent class also. 我的第一个冲动是说MyAwesomeSubclass.methods - ParentClass.methods
,但这会删除:method1
因为method1也在父类上定义了。 I need a list of all methods that are defined by a subclass. 我需要一个由子类定义的所有方法的列表。
Thanks for your help. 谢谢你的帮助。
Try MyAwesomeSubclass.instance_methods(false)
? 试试MyAwesomeSubclass.instance_methods(false)
吗? I believe that's what you're looking for... 我相信这就是您要寻找的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.