Get list of a class' instance methods Get list of a class' instance methods ruby ruby

Get list of a class' instance methods


You actually want TestClass.instance_methods, unless you're interested in what TestClass itself can do.

class TestClass  def method1  end  def method2  end  def method3  endendTestClass.methods.grep(/method1/) # => []TestClass.instance_methods.grep(/method1/) # => ["method1"]TestClass.methods.grep(/new/) # => ["new"]

Or you can call methods (not instance_methods) on the object:

test_object = TestClass.newtest_object.methods.grep(/method1/) # => ["method1"]


TestClass.methods(false) 

to get only methods that belong to that class only.

TestClass.instance_methods(false) would return the methods from your given example (since they are instance methods of TestClass).


TestClass.instance_methods

or without all the inherited methods

TestClass.instance_methods - Object.methods

(Was 'TestClass.methods - Object.methods')