Vladimir Tsukanov
Vladimir Tsukanov

Reputation: 4459

Get list of a class' instance methods

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

Upvotes: 110

Views: 122583

Answers (9)

noraj
noraj

Reputation: 4622

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:

Upvotes: 0

Philippe Perret
Philippe Perret

Reputation: 78

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

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

Upvotes: 0

nakwa
nakwa

Reputation: 1213

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)

Upvotes: 2

Mukesh Kumar Gupta
Mukesh Kumar Gupta

Reputation: 1647

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]

Upvotes: 5

Douglas G. Allen
Douglas G. Allen

Reputation: 2261

$ 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

Upvotes: 7

J-_-L
J-_-L

Reputation: 9177

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

Upvotes: 5

Bijan
Bijan

Reputation: 6772

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

Upvotes: 132

Pavling
Pavling

Reputation: 3963

TestClass.instance_methods

or without all the inherited methods

TestClass.instance_methods - Object.methods

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

Upvotes: 42

Andrew Grimm
Andrew Grimm

Reputation: 81510

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

Upvotes: 127

Related Questions