# Get only the non-inherited methods of a class

**URL:** <https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869>\
**Category:** ruby-talk\
**Created:** [2 November 2007 12:05 UTC](https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869 "2007-11-02T12:05:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [2 November 2007 12:05 UTC](https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869/1 "2007-11-02T12:05:57Z")

</div>

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|  
&nbsp;&nbsp;method = request.class.method(m)  
&nbsp;&nbsp;puts " #{m} = #{method.call}, "  
end

This would produce

accepts = text/htmlapplication/xmlimage/pngtext/plain\*/\*  
delete? = false  
etc

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.

Any ideas, anyone?

thanks  
max

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [2 November 2007 13:13 UTC](https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869/2 "2007-11-02T13:13:57Z")

</div>

Hi,

> **···**
>
> In message "Re: Get only the non-inherited methods of a class" on Fri, 2 Nov 2007 21:05:57 +0900, Max Williams \<toastkid.williams@gmail.com\> writes:
> 
> > Does anyone know how to get the methods of a class in such a way as to  
> > \*not\* get inherited ones?
> 
> Giving false to #methods.
> 
> &nbsp;&nbsp;Stringh.methods  
> &nbsp;&nbsp;=\> ["inspect", "send", "class\_eval",...  
> &nbsp;&nbsp;String.methods(false)  
> &nbsp;&nbsp;=\>
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [2 November 2007 13:22 UTC](https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869/3 "2007-11-02T13:22:26Z")

</div>

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.

Kind regards

robert

> **···**
>
> 2007/11/2, Yukihiro Matsumoto \<matz@ruby-lang.org\>:
> 
> > Hi,
> > 
> > In message "Re: Get only the non-inherited methods of a class" \> on Fri, 2 Nov 2007 21:05:57 +0900, Max Williams \<toastkid.williams@gmail.com\> writes:
> > 
> > \>Does anyone know how to get the methods of a class in such a way as to  
> > \>\*not\* get inherited ones?
> > 
> > Giving false to #methods.
> > 
> > &nbsp;&nbsp;Stringh.methods  
> > &nbsp;&nbsp;=\> ["inspect", "send", "class\_eval",...  
> > &nbsp;&nbsp;String.methods(false)  
> > &nbsp;&nbsp;=\>
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [2 November 2007 13:29 UTC](https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869/4 "2007-11-02T13:29:15Z")

</div>

Yukihiro Matsumoto wrote:

> Giving false to #methods.
> 
> &nbsp;&nbsp;Stringh.methods  
> &nbsp;&nbsp;=\> ["inspect", "send", "class\_eval",...  
> &nbsp;&nbsp;String.methods(false)  
> &nbsp;&nbsp;=\>

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)

Would you mind explaining what passing false to methods does?

thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [2 November 2007 13:59 UTC](https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869/5 "2007-11-02T13:59:35Z")

</div>

Hi,

> 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

&nbsp;&nbsp;C.instance\_methods(false)

> Would you mind explaining what passing false to methods does?

It means to list methods defined in the particular class.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

> **···**
>
> In message "Re: Get only the non-inherited methods of a class" on Fri, 2 Nov 2007 22:29:15 +0900, Max Williams \<toastkid.williams@gmail.com\> writes:

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [2 November 2007 14:07 UTC](https://rubytalk.org/t/get-only-the-non-inherited-methods-of-a-class/41869/6 "2007-11-02T14:07:06Z")

</div>

Yukihiro Matsumoto wrote:

> I thought you want class methods of String class. If you want to get  
> instance\_methods of a class C, call
> 
> &nbsp;&nbsp;C.instance\_methods(false)

Fantastic, thanks! And thanks for Ruby 😃

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
