Including a class.

O

Ola Bini

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
def initialize(config={})
self.singleton_class.module_eval do
if config[:foo]
partial_include Foo, :method1, :method7
elsif config[:bar]
partial_include Bar, :method1, :method7
end
end
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? =)

--
Ola Bini (http://ola-bini.blogspot.com)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

"Yields falsehood when quined" yields falsehood when quined.
 
M

Mauricio Fernandez

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. ========================
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
def initialize(config = {})
if config[:A]
extend A
elsif config[:B]
extend B
end
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.]
 
O

Ola Bini

Mauricio said:
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. ========================
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...)

--
Ola Bini (http://ola-bini.blogspot.com)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

"Yields falsehood when quined" yields falsehood when quined.
 
E

Eero Saynatkari

Ola said:
Mauricio said:
inherit the other class. [...]

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.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top