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.

Share:
111,691

Related videos on Youtube

Vladimir Tsukanov
Author by

Vladimir Tsukanov

Ruby, Java developer

Updated on September 29, 2020

Comments

  • Vladimir Tsukanov
    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
    sawa almost 13 years
    or without inherited methods: TestClass.methods(false).
  • Vladimir Tsukanov
    Vladimir Tsukanov almost 13 years
    @sawa TestClass.methods(false) returns empty
  • Phrogz
    Phrogz almost 13 years
    Heh, you beat me to it by 47 seconds. +1
  • Phrogz
    Phrogz almost 13 years
    This answer is wrong; the results will not include method1, method2, or method3, as those are methods of instances of the class, not methods of the TestClass object itself.
  • Vladimir Tsukanov
    Vladimir Tsukanov almost 13 years
    When i try print TestClass.new.instance_methods, i get this error my_class.rb:10:in <main>: undefined method instance_methods for #<TestClass:0x96b9d20> (NoMethodError)
  • oligan
    oligan almost 13 years
    You only need to do TestClass.new.methods. Maybe "it" was ambiguous in my answer.
  • Phrogz
    Phrogz almost 13 years
    Note that under Ruby 1.9+ the array of method names are symbols, not strings.
  • oligan
    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
    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
    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
    Vladimir Tsukanov almost 13 years
    @Phrogz - this answer works for me, i get [:method1, :method2, :method3] with print TestClass.new.methods - Object.methods
  • Phrogz
    Phrogz almost 13 years
    @Pavling Definitely edit your answer to be correct. (Quickly, before the OP assigns the credit to someone else! :)
  • oligan
    oligan almost 13 years
    @Phrogz: Keeping the (corrected) answer is also a good way of showing a potential "gotcha".
  • Daniel Doezema
    Daniel Doezema about 11 years
    This 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
    Aaron about 8 years
    Accepted answer is great but this one is likely what more people will come here for!
  • vinibrsl
    vinibrsl over 5 years
    This definitely should be the accepted answer. THANKS!
  • Jörg W Mittag
    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 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 even
  • Jörg W Mittag
    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
    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
    Aaron over 3 years
    I 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 and Object#instance_methods. I imagine that I found this answer's usage of the false argument to be what I was interested in as TestClass.instance_methods.size is 64 on Ruby 2.7.1 but TestClass.instance_methods(false).size is limited to the three defined. I probably was lazy and didn't want to chain grep 🤷‍♂️