From: James Coglan Date: 2008-09-10T07:30:01+09:00 Subject: Re: How to access a method in the parent class ------=_Part_31218_7012313.1220999776556 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 2008/9/9 I=F1aki Baz Castillo > 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. ------=_Part_31218_7012313.1220999776556--