From: "W. Kent Starr" Date: 2001-11-01T06:44:08+09:00 Subject: [ruby-talk:24037] Re: Overiding a method in another module Mark Hahn wrote: > > I think this won't work with instances of subclasses of original C1. > Ok, what am I missing/ (Lookingover my las post, not only is it likely I am not understanding your goal, it is definite my spellcheck doesn't catch my typos!) module M1 class C1 def m_1 p '1' end def m_2 p '2' end class C11 def m_1 p '111' end end end class C221 C2.new.m_2 # =>12 class C3 21 C3.new.m_2 # => 2 M1::C1.new.m_1 # => 1 class C4111 end C4.new.m_1 class C551 Regards, Kent Starr elderburn@mindspring.com > -----Original Message----- > From: wyzzrd@ruby-lang.org [mailto:wyzzrd@ruby-lang.org]On Behalf Of W. > Kent Starr > Sent: Wednesday, October 31, 2001 11:23 AM > To: ruby-talk ML > Subject: [ruby-talk:24018] Re: Overiding a method in another module > > Mark Hahn wrote: > > > > I implemented the following. I think it matches your suggestion. It > didn't > > work: > > > > module M1 > > class C1 > > def m_1 > > p '1' > > end > > end > > end > > include M1 > > > > module M2 > > module M1 > > class C1 > > def m_2 > > p '2' > > end > > end > > end > > end > > include M2 > > > > C1.new.m_2 -> error: method m_2 undefined > > > > -----Original Message----- > > From: jweirich@one.net [mailto:jweirich@one.net] > > Sent: Wednesday, October 31, 2001 8:26 AM > > To: ruby-talk ML > > Subject: [ruby-talk:23982] Re: Overiding a method in another module > > > > >>>>> "Mark" == Mark Hahn writes: > > > > Mark> Here is the situation that I am facing (see below). I want > > Mark> to add method m_2 to class C1 and make sure instances of C2 > > Mark> (any any other subclass of C1) respond to m_2. I cannot > > Mark> subclass C1 to accomplish this because I have no way to > > Mark> change C2 to subclass my new subclass. I cannot change the > > Mark> source of M1 because of the license. Even if there wasn't a > > Mark> license issue, isn't reusability all about using other code > > Mark> without changing it? > > > > Mark> module M1 << This is other person's module > > Mark> class C1 > > Mark> def m_1 > > Mark> ... > > Mark> end > > Mark> end > > Mark> class C2 < C1 > > Mark> ... > > Mark> end > > Mark> end > > > > Just do this in your code somewhere ... > > > > module M1 > > class C1 > > def m_2 > > ... > > end > > end > > end > > > > Ruby classes are open, so you can add new methods to them without > > touching the original source. > > > > -- > > -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich > > --------------------------------------------------------------------- > > "Beware of bugs in the above code; I have only proved it correct, > > not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas) > > IF i have corrected interpreted your goal (a bi IF), the following > works: > > module M1 > class C1 > def m_1 > p '1' > end > end > end > > # do not touch above ^ or license police will kill you! > > module M2 > include M1 > class C1 def m_2 > p '2' > end > end > end > > # did not touch M1 so am safe from license police! > > include M2 > > C1.new.m_1 # ==> 1 > C1.new.m_2 # ==> 2 > > Is this what you want? > > Regards, > > Kent Starr > elderburn@mindspring.com