1

Does anyone how to get all the methods that are defined or overwrited in current class rather than base class? e.g.

class MyBase
  def Test
  end
end

class MyDerived
  def Test1
  end
end

When I call MyDerived.methods, i got a lot of methods, but I only want to get 'Test1' because it is its own method, is it possible? thanks.

aaron
  • 1,951
  • 3
  • 27
  • 41

1 Answers1

5
class Foo
  def bar
  end
end

Foo.new.public_methods false

=> [:bar]
Yossi
  • 11,778
  • 2
  • 53
  • 66
  • You can also call Foo.new.method(:bar) to see where the method :bar was defined. You can also take advantage of how ruby subtracts arrays to, for example, see the methods added since a particular ancestors, e.g. Foo.new.public_methods(false) - Object.new.public_methods(false), or something like Foo.public_methods - (Foo.ancestors - Foo.included_modules)[1].public_methods see http://stackoverflow.com/questions/8138304/is-there-a-way-in-ruby-to-print-out-the-public-methods-of-an-object/8156584#8156584 – BF4 Dec 25 '11 at 15:39