checking if a method is defined on the class

后端 未结 3 1512
有刺的猬
有刺的猬 2021-02-01 03:30

How do I check if a method is defined on some class directly, not by inheritance or by inclusion/extension? I want something like \'foo?\' in the following:

clas         


        
3条回答
  •  广开言路
    2021-02-01 03:52

    For objects you can use Object.respond_to?.

    Returns true if obj responds to the given method.

    For classes take a look at Module.instance_methods

    Returns an array containing the names of the public and protected instance methods in the receiver.

提交回复
热议问题