Get list of a class' instance methods
111,691
Solution 1
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"]
Solution 2
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).
Solution 3
TestClass.instance_methods
or without all the inherited methods
TestClass.instance_methods - Object.methods
(Was 'TestClass.methods - Object.methods')
Solution 4
$ 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
Solution 5
You can get a more detailed list (e.g. structured by defining class) with gems like debugging or looksee.
Related videos on Youtube
Comments
-
Vladimir Tsukanov over 3 years
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
)? -
sawa almost 13 yearsor without inherited methods:
TestClass.methods(false)
. -
Vladimir Tsukanov almost 13 years@sawa
TestClass.methods(false)
returns empty -
Phrogz almost 13 yearsHeh, you beat me to it by 47 seconds. +1
-
Phrogz almost 13 yearsThis answer is wrong; the results will not include
method1
,method2
, ormethod3
, as those are methods of instances of the class, not methods of theTestClass
object itself. -
Vladimir Tsukanov almost 13 yearsWhen i try
print TestClass.new.instance_methods
, i get this errormy_class.rb:10:in <main>: undefined method instance_methods for #<TestClass:0x96b9d20> (NoMethodError)
-
oligan almost 13 yearsYou only need to do
TestClass.new.methods
. Maybe "it" was ambiguous in my answer. -
Phrogz almost 13 yearsNote that under Ruby 1.9+ the array of method names are symbols, not strings.
-
oligan almost 13 years@Phrogz: Yes, but you're allowed to use regular expressions on them. You don't even summon Cthulhu! :) Though you would get
[:method1]
instead. -
Pavling almost 13 years@Phrogz: Oops... yes, 'TestClass.instance_methods - Object.methods', or 'TestClass.new.methods - Object.methods'... that'll teach me for not firing up a console. Is it best for me to delete this answer, or edit it?
-
sawa almost 13 years@Vladimir @Phrogz The question was contradictory. In the title (before the edit by Theo), it was asking for class methods, and in the body, it's asking for
method1
, etc. -
Vladimir Tsukanov almost 13 years@Phrogz - this answer works for me, i get [:method1, :method2, :method3] with
print TestClass.new.methods - Object.methods
-
Phrogz almost 13 years@Pavling Definitely edit your answer to be correct. (Quickly, before the OP assigns the credit to someone else! :)
-
oligan almost 13 years@Phrogz: Keeping the (corrected) answer is also a good way of showing a potential "gotcha".
-
Daniel Doezema about 11 yearsThis is great way to test if a class has polymorphed methods from a pseudo-Interface/Abstract base class without having to try and call the methods directly.
-
Aaron about 8 yearsAccepted answer is great but this one is likely what more people will come here for!
-
vinibrsl over 5 yearsThis definitely should be the accepted answer. THANKS!
-
Jörg W Mittag over 3 years@Aaron: I highly doubt that, since this answer is wrong.
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 thatTestClass.methods
returns the list of methods you can call onTestClass
, which are the methods that are defined in its class. What isTestClass
's class? It isClass
. So,TestClass.methods
gives you the methods that are defined inClass
, not the ones that are defined inTestClass
. Not sure how this answer got 116 upvotes, when anybody with even -
Jörg W Mittag over 3 years… the most remote passing knowledge would instantly recognize it as wrong. Or, you know, just copy&pasting this code and running it would reveal that it is wrong.
-
Jörg W Mittag over 3 years@ViniciusBrasil: This answer is wrong, so it should definitely not be the accepted answer, and the fact that it has 116 upvotes is seriously sad, considering that it takes 1 second to just copy&paste the code and try it out, and less than one second to just think it through.
-
Aaron over 3 yearsI can't recall what was going on 4+ years ago so I'll offer my best guess. The currently accepted answer is thorough and provides a great understanding of the differences between
Object#methods
andObject#instance_methods
. I imagine that I found this answer's usage of thefalse
argument to be what I was interested in asTestClass.instance_methods.size
is 64 on Ruby 2.7.1 butTestClass.instance_methods(false).size
is limited to the three defined. I probably was lazy and didn't want to chaingrep
🤷♂️