# How to access a method in the parent class

**URL:** <https://www.ruby-forum.com/t/how-to-access-a-method-in-the-parent-class/148163>\
**Category:** Ruby\
**Created:** [September 10, 2008, 12:13am UTC](https://www.ruby-forum.com/t/how-to-access-a-method-in-the-parent-class/148163 "2008-09-10T00:13:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 12:13am UTC](https://www.ruby-forum.com/t/how-to-access-a-method-in-the-parent-class/148163/1 "2008-09-10T00:13:29Z")

</div>

Hi, I’ll explain my problem with an example code:

* * *

module M  
def hello  
puts “hello !!!”  
B.new  
end

class B  
def initialize  
puts “B instance initialized !!!”  
bye() \<— ERROR !!!  
end  
end  
end

class A  
include M

def bye  
puts “bye !!!”  
end  
end

## a = A.new a.hello

I want the following behaviour:

irb\> a.hello  
=\> “hello !!!”  
=\> “B instance initialized !!!”  
=\> “bye !!!”

But the last bye() gives an error since, of course, “bye” is not defined  
in B  
class, but in A class.

## I know that if it would be:

module M

def hello  
puts “hello !!!”  
bye  
end

end

## irb\> a.hellp

This works since “bye()” calls the “bye” method in class A. But this is  
not my  
case.

How could I call A#bye method from M::B class?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 12:37am UTC](https://www.ruby-forum.com/t/how-to-access-a-method-in-the-parent-class/148163/2 "2008-09-10T00:37:29Z")

</div>

2008/9/9 IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> ```
> def initialize
> def bye
> 
> ```
> 
> I want the following behaviour:
> 
> ## irb\> a.hellp
> 
> This works since “bye()” calls the “bye” method in class A. But this is not  
> my  
> case.
> 
> How could I call A#bye method from M::B class?
> 
> Thanks a lot.

To call a method from another class in the context of an instance of B,  
put  
this inside M::B#initialize:

A.instance\_method(:bye).bind(self).call()

Hard to tell from this code, seeing as how it does nothing apparently  
useful, but if you find yourself doing this it may be a sign that you  
need  
to rethink your design.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 12:45am UTC](https://www.ruby-forum.com/t/how-to-access-a-method-in-the-parent-class/148163/3 "2008-09-10T00:45:05Z")

</div>

Hi –

On Wed, 10 Sep 2008, James C. wrote:

> > ```
> > include M
> > 
> > ```
> > 
> > B
> > 
> > Thanks a lot.
> 
> To call a method from another class in the context of an instance of B, put  
> this inside M::B#initialize:
> 
> A.instance\_method(:bye).bind(self).call()

That won’t work here, because you’d need an instance of A (or a  
subclass of A).

> Hard to tell from this code, seeing as how it does nothing apparently  
> useful, but if you find yourself doing this it may be a sign that you need  
> to rethink your design.

It definitely looks a bit inside-out: A requires that M exist, but A  
is essentially trying to serve as a superclass for M::B. Sort of like  
this:

module M  
def hello  
puts “hello !!!”  
B.new  
end  
end

class A  
include M  
def bye  
puts “bye !!!”  
end  
end

module M  
class B \< A  
def initialize  
puts “B instance initialized !!!”  
bye  
end  
end  
end

which works, but does indeed look like it would need some ironing out  
in the design.

David

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 12:49am UTC](https://www.ruby-forum.com/t/how-to-access-a-method-in-the-parent-class/148163/4 "2008-09-10T00:49:46Z")

</div>

El MiÃ©rcoles, 10 de Septiembre de 2008, David A. Black escribiÃ³:

> Â Â Â puts “bye !!!”  
> end
> 
> which works, but does indeed look like it would need some ironing out  
> in the design.

Thanks a lot. Maybe I should re-desing the code to avoid using things so  
exotic… 🙂

Really thanks a lot to both.
