How do I inspect the methods of a Ruby object?

前端 未结 3 1641
Happy的楠姐
Happy的楠姐 2021-02-02 12:06

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 cla

3条回答
  •  独厮守ぢ
    2021-02-02 12:21

    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:

    # obj is the current object
    parent = obj.class.superclass
    
    methods = (obj.methods - parent.methods)
    

提交回复
热议问题