110

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)?

0

9 Answers 9

132
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).

Sign up to request clarification or add additional context in comments.

7 Comments

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.
Accepted answer is great but this one is likely what more people will come here for!
This definitely should be the accepted answer. THANKS!
@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
… 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.
|
127

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"]

5 Comments

Heh, you beat me to it by 47 seconds. +1
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)
You only need to do TestClass.new.methods. Maybe "it" was ambiguous in my answer.
Note that under Ruby 1.9+ the array of method names are symbols, not strings.
@Phrogz: Yes, but you're allowed to use regular expressions on them. You don't even summon Cthulhu! :) Though you would get [:method1] instead.
42
TestClass.instance_methods

or without all the inherited methods

TestClass.instance_methods - Object.methods

(Was 'TestClass.methods - Object.methods')

8 Comments

or without inherited methods: TestClass.methods(false).
@sawa TestClass.methods(false) returns empty
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.
@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?
@Pavling Definitely edit your answer to be correct. (Quickly, before the OP assigns the credit to someone else! :)
|
7
$ 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

Comments

5

You can get a more detailed list (e.g. structured by defining class) with gems like debugging or looksee.

Comments

5

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]

Comments

2

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)

Comments

0

If you have an instance but don't know the class name:

object.class.instance_methods(false)
# => Array of class instance methods

Comments

0

Class methods

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)

Instance methods

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)

Filtering

By default, all kind of methods are returned, but what if you want only public methods? There are specialized variants for that:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.