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:
Returns an array containing the names of the public and protected
instance methods in the receiver. For a module, these are the public
and protected methods; for a class, they are the instance (not
singleton) methods. If the optional parameter is false, the methods of
any ancestors are not included.
I am taking the official documentation example.
module A
def method1()
puts "method1 say hi"
end
end
class B
include A #mixin
def method2()
puts "method2 say hi"
end
end
class C < B #inheritance
def method3()
puts "method3 say hi"
end
end
Let's see the output.
A.instance_methods(false)
=> [:method1]
A.instance_methods
=> [:method1]
B.instance_methods
=> [:method2, :method1, :nil?, :===, ...# ] # methods inherited from parent class, most important :method1 is also visible because we mix module A in class B
B.instance_methods(false)
=> [:method2]
C.instance_methods
=> [:method3, :method2, :method1, :nil?, :===, ...#] # same as above
C.instance_methods(false)
=> [:method3]
class TestClass
class << self
def own_methods
self.instance_methods - self.superclass.instance_methods
end
end
end
TestClass.own_methods
=> [:method1, :method2, :method3]