From: Enrique Comba Riepenhausen Date: 2007-05-12T00:17:45+09:00 Subject: Re: Is there a possibility to include methods of a class? On 11 May 2007, at 17:14, Brian Candler wrote: > On Fri, May 11, 2007 at 10:29:01PM +0900, Bernd wrote: >> Hi all, >> first of all: this list and you rock! I know that there is no >> multiple >> inheritance, but you can use the concept of mixin. >> However, I want to inherit my class another and "mixin" methods from >> another class. >> To be more precise, >> I have a class that inherits another one (that comes from a >> library and >> I do not want to change) and I want the methods of >> ActiveRecord::Base in >> my class. Is there a way to do that (without copying and paste the >> whole >> ActiveRecord::Base class and changing it to a module)? > > I would use delegation for one class or the other. > > That is: define your class as > > class Foo < ActiveRecord::Base > .. > def initialize > @other = Library::Bar.new > end > end > > Or vice versa, whichever makes more sense. And if you can't choose, > then > delegate to both. > > class FooDB < ActiveRecord::Base > .. more stuff > end > > class Foo > def initialize(id) > @db = FooDB.find(id) > @lib = Library::Bar.new > end > end > > Then the interface that Foo exposes to the world can pass some > calls to @db, > some to @lib, or (the best news) can combine behaviours from both in > whichever way it likes. > That would actually be a so called Bridge Design Pattern... I like it I think I will use it for Ruby Patterns ;) Thanks Brian!