From: Mark Hahn Date: 2001-11-01T04:39:35+09:00 Subject: [ruby-talk:24021] Re: Overiding a method in another module I think this won't work with instances of subclasses of original C1. -----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 1 C1.new.m_2 # ==> 2 Is this what you want? Regards, Kent Starr elderburn@mindspring.com