From: Mark Hahn Date: 2001-11-01T01:57:20+09:00 Subject: [ruby-talk:23984] Re: Overiding a method in another module 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)