# Public\_methods on a class

**URL:** <https://rubytalk.org/t/public-methods-on-a-class/68275>\
**Category:** ruby-talk\
**Created:** [1 August 2013 22:51 UTC](https://rubytalk.org/t/public-methods-on-a-class/68275 "2013-08-01T22:51:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [1 August 2013 22:51 UTC](https://rubytalk.org/t/public-methods-on-a-class/68275/1 "2013-08-01T22:51:04Z")

</div>

Hi

is anyone able to to explain to me why :foo is in the array below?

My understanding of the docs would suggest that calling  
public\_methods(false) should only include methods defined on the class  
itself?

> > class A  
> > &nbsp;&nbsp;def self.foo  
> > &nbsp;&nbsp;&nbsp;&nbsp;"FOO"  
> > &nbsp;&nbsp;end  
> > end  
> > class B \< A  
> > end  
> > p B.public\_methods(false)

=\> [:foo, :allocate, :new, :superclass]

([Eval - latest in tech, AI, software, Networking, Marketing & more](https://eval.in/40208%5C))

Thanks

Steve

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

---

<div class="post-metadata">

**Author:** ![Ganesh\_Kaliannan](https://avatars.discourse-cdn.com/v4/letter/g/ea5d25/32.png) [@Ganesh\_Kaliannan](https://rubytalk.org/u/Ganesh_Kaliannan)\
**Post date:** [2 August 2013 02:27 UTC](https://rubytalk.org/t/public-methods-on-a-class/68275/2 "2013-08-02T02:27:44Z")

</div>

In the defination of class B, the part “\< A" tells ruby that class B  
is inheriting from class A.

When class B inherits from class A, all the properties(methods,  
instance and class variables) in class A are available to class B

Ex:

class A  
&nbsp;&nbsp;&nbsp;def self.foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"FOO"  
&nbsp;&nbsp;&nbsp;end  
end  
&nbsp;&nbsp;class B \< A

&nbsp;&nbsp;end

class C \< B

end

C.public\_methods(false)

[:foo, :allocate, :new, :superclass]

When we inherit class B in C will result the same...

Thanks,  
Ganesh Kaliannan

> **···**
>
> On Fri, Aug 2, 2013 at 4:21 AM, Steve S. \<lists@ruby-forum.com\> wrote:
> 
> > Hi
> > 
> > is anyone able to to explain to me why :foo is in the array below?
> > 
> > My understanding of the docs would suggest that calling  
> > public\_methods(false) should only include methods defined on the class  
> > itself?
> > 
> > > > class A  
> > > > &nbsp;&nbsp;def self.foo  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;"FOO"  
> > > > &nbsp;&nbsp;end  
> > > > end  
> > > > class B \< A  
> > > > end  
> > > > p B.public\_methods(false)
> > 
> > =\> [:foo, :allocate, :new, :superclass]
> > 
> > ([Eval - latest in tech, AI, software, Networking, Marketing & more](https://eval.in/40208%5C))
> > 
> > Thanks
> > 
> > Steve
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [2 August 2013 07:37 UTC](https://rubytalk.org/t/public-methods-on-a-class/68275/3 "2013-08-02T07:37:03Z")

</div>

The part I don't understand is behaviour of public\_methods.

From the docs:

"  
If the all parameter is set to false, only those methods in the receiver  
will be listed.  
"

So, from my understanding, in my example, the Inheritance chain for A  
is:

"A \< Object"

And B therefore is:

"B \< A \< Object"

What I don't understand is why A.public\_methods(false) excludes the  
public\_methods defined on Object, but B.public\_methods(false) INCLUDES  
the public methods defined on A.

Thanks

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

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 August 2013 05:40 UTC](https://rubytalk.org/t/public-methods-on-a-class/68275/4 "2013-08-05T05:40:20Z")

</div>

It seems that A.public\_methods(false) does include all the public  
methods.

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