I have a class:
class TestClass
def method1
end
def method2
end
def method3
end
end
How can I get a list of my methods in this class (method1, method2, method3)?
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).
Object#methods returns the methods that you can call on an object. What are the methods you can call on an object? The ones that are defined in its class. Which means that TestClass.methods returns the list of methods you can call on TestClass, which are the methods that are defined in its class. What is TestClass's class? It is Class. So, TestClass.methods gives you the methods that are defined in Class, not the ones that are defined in TestClass. Not sure how this answer got 116 upvotes, when anybody with evenYou 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"]
print TestClass.new.instance_methods, i get this error my_class.rb:10:in <main>: undefined method instance_methods for #<TestClass:0x96b9d20> (NoMethodError)TestClass.new.methods. Maybe "it" was ambiguous in my answer.[:method1] instead.TestClass.instance_methods
or without all the inherited methods
TestClass.instance_methods - Object.methods
(Was 'TestClass.methods - Object.methods')
TestClass.methods(false).TestClass.methods(false) returns emptymethod1, method2, or method3, as those are methods of instances of the class, not methods of the TestClass object itself.According to Ruby Doc instance_methods
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]
To get only own methods, and exclude inherited ones:
From within the instance:
self.methods - self.class.superclass.instance_methods
From outside:
TestClass.instance_methods - TestClass.superclass.instance_methods
Add it to the class:
class TestClass
class << self
def own_methods
self.instance_methods - self.superclass.instance_methods
end
end
end
TestClass.own_methods
=> [:method1, :method2, :method3]
(with ruby 2.6.x)
List class methods without inherited methods with Object#methods(regular=true) :
Returns a list of the names of public and protected methods of obj. This will include all the methods accessible in obj’s ancestors. If the optional parameter is false, it returns an array of obj’s public and protected singleton methods, the array will not include methods in modules included in obj.
TestClass.methods(false)
List instance methods without inherited methods with Module#instance_methods(include_super=true):
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.
TestClass.instance_methods(false)
By default, all kind of methods are returned, but what if you want only public methods? There are specialized variants for that: