Does anyone know how to get the methods of a class in such a way as to not get inherited ones?
For example, the Rails class AbstractRequest has 20 or so methods, all
of which can be called with no parameters. I can call them all,
displaying the name of the method, and the result of calling it with an
empty parameter list, with
request.class.methods.each do |m|
method = request.class.method(m)
puts " #{m} = #{method.call}, "
end
However, calling the above also gets all the inherited methods as well,
many of which require parameters, and the code breaks because i’m not
supplying them.
Is there some sort of test that i can do to see if a class creates a
method, rather than inheriting it? I’m thinking of possible solutions,
like recursing up the superclass chain, and not calling the method if
any superclass responds to it, but this seems very complicated for such
a simple problem.
I guess when invoked on the class instance then it should probably be
String.instance_methods and String.instance_methods(false). If
invoked on the instance then methods and methods(false) should be
appropriate.
In message “Re: Get only the non-inherited methods of a class”
on Fri, 2 Nov 2007 22:29:15 +0900, Max W. [email protected] writes:
|Thanks matz, but why does that return an empty array? Shouldn’t it be
|
|[“%”, “*”, “+”, “<<”, etc]?
|
|(actually for me it returns [“yaml_new”, “yaml_tag_subclasses?”], each
|of which gives me a NoMethodError when i try to call it on a string)
I thought you want class methods of String class. If you want to get
instance_methods of a class C, call
C.instance_methods(false)
|Would you mind explaining what passing false to methods does?
It means to list methods defined in the particular class.