Michiel de Mare
Michiel de Mare

Reputation: 42420

List of instance methods of current class only

I have an instance o of class O. I'd like to know what o is capable of.

o.methods will give me many methods. So I usually do o.methods - Object.instance_methods. But that is not concise.

I want to do something like o.methods - o.class.superclass.instance_methods. That is, just the methods defined in O itself.

Is there some other way?

Upvotes: 11

Views: 2711

Answers (1)

toro2k
toro2k

Reputation: 19230

You can use the method Module#instance_methods:

o.class.instance_methods(false)


Warning The documentation seems to be wrong, it says that:

With no argument, or with an argument that is false, the instance methods in mod are returned, otherwise the methods in mod and mod’s superclasses are returned.

But actually the parameter by default is true:

String.instance_methods.size
# => 184
String.instance_methods(false).size
# => 130

Upvotes: 16

Related Questions