Get list of a class' instance methods
TestClass.instance_methods
or without all the inherited methods
TestClass.instance_methods - Object.methods
(Was 'TestClass.methods - Object.methods')
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).
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
end
end
TestClass.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.new
test_object.methods.grep(/method1/) # => ["method1"]
$ irb --simple-prompt
class TestClass
def method1
end
def method2
end
def method3
end
end
tc_list = TestClass.instance_methods(false)
#[:method1, :method2, :method3]
puts tc_list
#method1
#method2
#method3