# Calling functions in a child class

**URL:** <https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667>\
**Category:** Ruby\
**Created:** [October 26, 2006, 2:30am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667 "2006-10-26T02:30:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianspaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/82dd89/32.png) [@brianspaid](https://www.ruby-forum.com/u/brianspaid)\
**Post date:** [October 26, 2006, 2:30am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667/1 "2006-10-26T02:30:52Z")

</div>

I have a class:

class alpha  
def test\_method  
puts “test method”  
end

def get\_methods  
puts alpha.methods  
end  
end

running “get\_methods” here will return a long list of methods including  
test\_method. But, I also have this class:

class bravo \< alpha  
def another\_test\_method  
end  
end

i want to be able to call get\_methods in the parent class and have it  
return the functions of the child class as well. so in this example:

test\_obj = bravo.new  
bravo.get\_methods

would return all methods including

test\_method  
another\_test\_method

any ideas?

thanks,  
Brian

---

<div class="post-metadata">

**Author:** ![brianspaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/82dd89/32.png) [@brianspaid](https://www.ruby-forum.com/u/brianspaid)\
**Post date:** [October 26, 2006, 3:01am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667/2 "2006-10-26T03:01:03Z")

</div>

Brian Schlect wrote:

> end  
> return the functions of the child class as well. so in this example:
> 
> test\_obj = bravo.new  
> bravo.get\_methods
> 
> would return all methods including
> 
> test\_method  
> another\_test\_method
> 
> any ideas?

That’s an expensive operation. Something along the lines of:

ObjectSpace.each(alpha) { |k|  
p k.get\_methods  
}

In fact, that’s so exepnsive you may want to reconsider what you’re  
trying to do here.

T.

---

<div class="post-metadata">

**Author:** ![brianspaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/82dd89/32.png) [@brianspaid](https://www.ruby-forum.com/u/brianspaid)\
**Post date:** [October 26, 2006, 4:03am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667/3 "2006-10-26T04:03:09Z")

</div>

Hi –

On Thu, 26 Oct 2006, Brian Schlect wrote:

> end  
> It’s a little unclear what you’re doing here. “class alpha” won’t  
> parse (alpha isn’t a constant), and since test\_method is not defined  
> as a class method, it won’t appear when you do alpha.methods.

> test\_obj = bravo.new  
> bravo.get\_methods
> 
> would return all methods including
> 
> test\_method  
> another\_test\_method

See if this does what you need:

class Alpha  
def test\_method  
end

```
 def get_methods
   p self.class.instance_methods.sort # for easy viewing :-)
 end

```

end

Alpha.new.get\_methods

class Bravo \< Alpha  
def another\_test\_method  
end  
end

Bravo.new.get\_methods

David

---

<div class="post-metadata">

**Author:** ![brianspaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/82dd89/32.png) [@brianspaid](https://www.ruby-forum.com/u/brianspaid)\
**Post date:** [October 26, 2006, 6:01am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667/4 "2006-10-26T06:01:24Z")

</div>

Phrogz wrote:

> Or do you want the exact same list of methods returned regardless of  
> what level you call the method at?

Here’s an example showing this end result. (Look ma! A real use for  
class variables!)

class Alpha  
@@alpha\_and\_subs = [self]  
def self.inherited( subklass )  
@@alpha\_and\_subs \<\< subklass  
end

def self.all\_methods  
@@alpha\_and\_subs.map{ |klass|  
klass.instance\_methods( false )  
}.flatten  
end

def alpha1; end  
def alpha2; end  
end

class Bravo \< Alpha  
def bravo1; end  
end

class Charlie \< Bravo  
def charlie1; end  
end

class Bingo \< Alpha  
def bingo1; end  
end

p Alpha.all\_methods  
#=\> [“alpha1”, “alpha2”, “bravo1”, “charlie1”, “bingo1”]

p Bravo.all\_methods  
#=\> [“alpha1”, “alpha2”, “bravo1”, “charlie1”, “bingo1”]

p Charlie.all\_methods  
#=\> [“alpha1”, “alpha2”, “bravo1”, “charlie1”, “bingo1”]

---

<div class="post-metadata">

**Author:** ![brianspaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/82dd89/32.png) [@brianspaid](https://www.ruby-forum.com/u/brianspaid)\
**Post date:** [October 26, 2006, 7:09am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667/5 "2006-10-26T07:09:32Z")

</div>

Brian Schlect wrote:

> end  
> return the functions of the child class as well. so in this example:
> 
> test\_obj = bravo.new  
> bravo.get\_methods

Did you mean this?

test\_obj.get\_methods

Then a simple solution is:

class Alpha  
def test\_method  
puts “test method”  
end

def get\_methods(include\_methods\_inherited\_by\_alpha = false)  
if include\_methods\_inherited\_by\_alpha  
methods  
else  
methods - Alpha.superclass.instance\_methods  
end  
end  
end

alpha = Alpha.new  
p alpha.get\_methods  
#p alpha.get\_methods(true)  
puts

class Bravo \< Alpha  
def another\_test\_method  
end  
end

bravo = Bravo.new  
p bravo.get\_methods  
#p bravo.get\_methods(true)

**END**

Output:

[“get\_methods”, “test\_method”]

[“get\_methods”, “test\_method”, “another\_test\_method”]

---

<div class="post-metadata">

**Author:** ![brianspaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/82dd89/32.png) [@brianspaid](https://www.ruby-forum.com/u/brianspaid)\
**Post date:** [October 26, 2006, 8:22am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667/6 "2006-10-26T08:22:48Z")

</div>

That is what I was trying to do. Thanks alot. I mistyped my code  
example, but you were able to read through it. Thank you, Joel

---

<div class="post-metadata">

**Author:** ![brianspaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/82dd89/32.png) [@brianspaid](https://www.ruby-forum.com/u/brianspaid)\
**Post date:** [October 26, 2006, 5:45am UTC](https://www.ruby-forum.com/t/calling-functions-in-a-child-class/73667/7 "2006-10-26T05:45:23Z")

</div>

Brian Schlect wrote:

> i want to be able to call get\_methods in the parent class and have it  
> return the functions of the child class as well. so in this example:
> 
> test\_obj = bravo.new  
> bravo.get\_methods

I’m confused. Do you want Alpha.get\_methods to return Alpha’s and  
Bravo’s methods, and Bravo.get\_methods to return just Bravo’s methods?

Or do you want the exact same list of methods returned regardless of  
what level you call the method at?

Here’s a solution for the former (and, because I wonder if it’s  
possibly what you want, it only includes the instance methods defined  
by Alpha and Bravo):

class Alpha  
def self.inherited( subklass )  
@self\_and\_subklasses ||= [self]  
@self\_and\_subklasses \<\< subklass  
end

def self.all\_methods  
@self\_and\_subklasses ||= [self]  
@self\_and\_subklasses.map{ |klass|  
klass.instance\_methods( false )  
}.flatten  
end

def alpha\_test1; end  
def alpha\_test2; end  
end

class Bravo \< Alpha  
def bravo\_test1; end  
end

p Alpha.all\_methods  
#=\> [“alpha\_test1”, “alpha\_test2”, “bravo\_test1”]

p Bravo.all\_methods  
#=\> [“bravo\_test1”]
