How to access a method in the parent class

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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

=2D----------------------
module M
def hello
puts "hello !!!"
B.new
end
=09
class B
def initialize
puts "B instance initialized !!!"
bye() <--- ERROR !!!!
end
end
end


class A
include M

def bye
puts "bye !!!"
end
end


a =3D A.new
a.hello
=2D----------------------


I want the following behaviour:

irb> a.hello
=3D> "hello !!!"
=3D> "B instance initialized !!!"
=3D> "bye !!!"


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

I know that if it would be:
=2D-----------------
module M

def hello
puts "hello !!!"
bye
end

end

irb> a.hellp
=2D-----------------
This works since "bye()" calls the "bye" method in class A. But this is not=
my=20
case.

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

Thanks a lot.



=2D-=20
I=C3=B1aki Baz Castillo
 
J

James Coglan

2008/9/9 I=F1aki Baz Castillo said:
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 =3D A.new
a.hello
-----------------------


I want the following behaviour:

irb> a.hello
=3D> "hello !!!"
=3D> "B instance initialized !!!"
=3D> "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 n= ot
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.
 
I

Iñaki Baz Castillo

RWwgTWnDqXJjb2xlcywgMTAgZGUgU2VwdGllbWJyZSBkZSAyMDA4LCBEYXZpZCBBLiBCbGFjayBl
c2NyaWJpw7M6Cj4gbW9kdWxlIE0KPiDCoCDCoGRlZiBoZWxsbwo+IMKgIMKgIMKgcHV0cyAiaGVs
bG8gISEhIgo+IMKgIMKgIMKgQi5uZXcKPiDCoCDCoGVuZAo+IGVuZAo+Cj4gY2xhc3MgQQo+IMKg
IMKgaW5jbHVkZSBNCj4gwqAgwqBkZWYgYnllCj4gwqAgwqAgwqBwdXRzICJieWUgISEhIgo+IMKg
IMKgZW5kCj4gZW5kCj4KPiBtb2R1bGUgTQo+IMKgIMKgY2xhc3MgQiA8IEEKPiDCoCDCoCDCoGRl
ZiBpbml0aWFsaXplCj4gwqAgwqAgwqAgwqBwdXRzICJCIGluc3RhbmNlIGluaXRpYWxpemVkICEh
ISIKPiDCoCDCoCDCoCDCoGJ5ZQo+IMKgIMKgIMKgZW5kCj4gwqAgwqBlbmQKPiBlbmQKPgo+IHdo
aWNoIHdvcmtzLCBidXQgZG9lcyBpbmRlZWQgbG9vayBsaWtlIGl0IHdvdWxkIG5lZWQgc29tZSBp
cm9uaW5nIG91dAo+IGluIHRoZSBkZXNpZ24uCgpUaGFua3MgYSBsb3QuIE1heWJlIEkgc2hvdWxk
IHJlLWRlc2luZyB0aGUgY29kZSB0byBhdm9pZCB1c2luZyB0aGluZ3Mgc28gCmV4b3RpYy4uLiA6
KQoKUmVhbGx5IHRoYW5rcyBhIGxvdCB0byBib3RoLgoKLS0gCknDsWFraSBCYXogQ2FzdGlsbG8K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top