# Including a class

**URL:** <https://rubytalk.org/t/including-a-class/30434>\
**Category:** ruby-talk\
**Created:** [29 August 2006 08:02 UTC](https://rubytalk.org/t/including-a-class/30434 "2006-08-29T08:02:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ola\_Bini3](https://avatars.discourse-cdn.com/v4/letter/o/85e7bf/32.png) [@Ola\_Bini3](https://rubytalk.org/u/Ola_Bini3)\
**Post date:** [29 August 2006 08:02 UTC](https://rubytalk.org/t/including-a-class/30434/1 "2006-08-29T08:02:10Z")

</div>

Hi,

As an extension on my earlier question of partial includes; I just noticed that you can't include a Class, even though it is a Module.

Anyone have a way of doing this?

What I would like to do, is to be able to dynamically mix in some good methods from preexisting classes, depending on arguments to the constructor. Since I need to do it dynamically, and also need it differently for different instances of the class, I can't make it inherit the other class.

What I would like is something like this; say I have class Foo and Bar, with many methods in them.

class Baz \< AnotherAbstractBaseClass  
&nbsp;&nbsp;&nbsp;def initialize(config={})  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.singleton\_class.module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if config[:foo]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;partial\_include Foo, :method1, :method7  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif config[:bar]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;partial\_include Bar, :method1, :method7  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

Baz.new(:bar =\> true).method1

But I can't have an instance of Bar or Foo so delegating is impossible. I need the methods to be in the the same instance. Any help? =)

> **···**
>
> --  
> &nbsp;&nbsp;Ola Bini ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
> &nbsp;&nbsp;JvYAML, RbYAML, JRuby and Jatha contributor  
> &nbsp;&nbsp;System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
> &nbsp;&nbsp;OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))
> 
> &nbsp;&nbsp;"Yields falsehood when quined" yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernandez](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Mauricio\_Fernandez](https://rubytalk.org/u/Mauricio_Fernandez)\
**Post date:** [29 August 2006 08:39 UTC](https://rubytalk.org/t/including-a-class/30434/2 "2006-08-29T08:39:52Z")

</div>

> As an extension on my earlier question of partial includes; I just  
> noticed that you can't include a Class, even though it is a Module.
> 
> Anyone have a way of doing this?
> 
> What I would like to do, is to be able to dynamically mix in some good  
> methods from preexisting classes, depending on arguments to the  
> constructor. Since I need to do it dynamically, and also need it  
> differently for different instances of the class, I can't make it  
> inherit the other class.

[...]

> **···**
>
> On Tue, Aug 29, 2006 at 05:02:10PM +0900, Ola Bini wrote:
> 
> > But I can't have an instance of Bar or Foo so delegating is impossible.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;========================
> 
> > I need the methods to be in the the same instance.
> 
> Doesn't this provide a very strong indication that you want to put the  
> reusable methods in modules? 🙂
> 
> RUBY\_VERSION # =\> "1.8.5"  
> RUBY\_RELEASE\_DATE # =\> "2006-08-25"  
> module A; def foo; "A#foo" end end  
> module B; def foo; "B#foo" end end  
> class X; include A end  
> class Y; include B end  
> class Z  
> &nbsp;&nbsp;def initialize(config = {})  
> &nbsp;&nbsp;&nbsp;&nbsp;if config[:A]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extend A  
> &nbsp;&nbsp;&nbsp;&nbsp;elsif config[:B]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extend B  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> Z.new(:A =\> true).foo # =\> "A#foo"  
> Z.new(:B =\> true).foo # =\> "B#foo"
> 
> [See [211308] for a Module#append\_from implementation that doesn't clobber  
> inherited methods.]
> 
> --  
> Mauricio Fernandez - [http://eigenclass.org](http://eigenclass.org) - singular Ruby

---

<div class="post-metadata">

**Author:** ![Ola\_Bini3](https://avatars.discourse-cdn.com/v4/letter/o/85e7bf/32.png) [@Ola\_Bini3](https://rubytalk.org/u/Ola_Bini3)\
**Post date:** [29 August 2006 08:54 UTC](https://rubytalk.org/t/including-a-class/30434/3 "2006-08-29T08:54:29Z")

</div>

Mauricio Fernandez wrote:

> > As an extension on my earlier question of partial includes; I just noticed that you can't include a Class, even though it is a Module.
> > 
> > Anyone have a way of doing this?
> > 
> > What I would like to do, is to be able to dynamically mix in some good methods from preexisting classes, depending on arguments to the constructor. Since I need to do it dynamically, and also need it differently for different instances of the class, I can't make it inherit the other class.
> 
> [...]
> 
> > But I can't have an instance of Bar or Foo so delegating is impossible.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;========================
> 
> > I need the methods to be in the the same instance.
> 
> Doesn't this provide a very strong indication that you want to put the  
> reusable methods in modules? 🙂

See, that's my problem. I have no control over them. Let me explain. I'm working on ActiveRecord-JDBC, and sorry to say, some features of JDBC aren't really database independent (for example, DDL). So, my current track is to include parts of the original implementation to avoid this problem. Right now I have copied the source verbatim, but that is no solution. I want AR-JDBC to recognize that it's being used with an Oracle JDBC driver, and then include some parts of OracleAdapter, (but only parts...)

> **···**
>
> > On Tue, Aug 29, 2006 at 05:02:10PM +0900, Ola Bini wrote:
> 
> --  
> &nbsp;&nbsp;Ola Bini ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
> &nbsp;&nbsp;JvYAML, RbYAML, JRuby and Jatha contributor  
> &nbsp;&nbsp;System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
> &nbsp;&nbsp;OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))
> 
> &nbsp;&nbsp;"Yields falsehood when quined" yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![E\_S](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@E\_S](https://rubytalk.org/u/E_S)\
**Post date:** [29 August 2006 14:43 UTC](https://rubytalk.org/t/including-a-class/30434/4 "2006-08-29T14:43:52Z")

</div>

Ola Bini wrote:

> Mauricio Fernandez wrote:
> 
> > > inherit the other class.
> > 
> > [...]
> > 
> > > But I can't have an instance of Bar or Foo so delegating is impossible.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;========================
> > 
> > > I need the methods to be in the the same instance.
> > 
> > Doesn't this provide a very strong indication that you want to put the  
> > reusable methods in modules? 🙂
> 
> See, that's my problem. I have no control over them. Let me explain. I'm  
> working on ActiveRecord-JDBC, and sorry to say, some features of JDBC  
> aren't really database independent (for example, DDL). So, my current  
> track is to include parts of the original implementation to avoid this  
> problem. Right now I have copied the source verbatim, but that is no  
> solution. I want AR-JDBC to recognize that it's being used with an  
> Oracle JDBC driver, and then include some parts of OracleAdapter, (but  
> only parts...)

Will simple composition and delegation work?

> **···**
>
> > --  
> > &nbsp;&nbsp;Ola Bini ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
