From: Bernd Burnt Date: 2007-05-12T00:30:00+09:00 Subject: Re: Is there a possibility to include methods of a class? 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. This is the workaround I just had so far, and now I want to get rid of it. I have two classes: class Myclass < ActiveRecord::Base def self.find(number) Anotherclass.find(number) end class Anotherclass < SAP4Rails::Base def self.find(number) do something end But this solution to me seems to be so un-ruby! As SAP4Rails::Base does not have an explicit superclass, I wondered, if there is some way to inject it dynamically a super class, because I need some other methods from ActiveRecord in another application I know that I could just alter the SAP4Rails class, but I want it in the way, that you just need ActiveRecord, SAP4RAils and my piece of code. -- Posted via http://www.ruby-forum.com/.