mixing in modules ?

K

konsu

hello,

given:

1. class C that implements some arbitrary methods
2. a database with code fragments stored as text

i create a variable 'var' of class C and then read some code fragments from
the database. each fragment i read is evaluated in a dynamic anonymous
module, and this module is used to extend 'var':

var = C.new
.. . .
m = Module.new
m.module_eval(code_fragment)
.. . .
var.extend(m)

so if a fragment implements some methods that are already defined in C
mixing in effectively replaces the original methods with the ones defined in
the fragment.

the problem:

class C defines an instance variable @x:

class C
def initialize; @x = 'C'; end
def to_s; @x; end
end

i want to override 'to_s' method. but the new implementation that i have in
my fragment still needs to refer to @x. so i do this:

fragment = "def to_s; foo(@x); end"

this works fine. but this does not seem like a good programming style. a
module refers to a variable that is defined in the object that this module
happens to be mixed in to. can anyone comment on this? basically the problem
is to be able to override the behaviour of 'var' at run time as code
fragments are read.

to give a concrete example of what i am trying to do, suppose you have a
database with text fields. each field is represented at run time by class C
that can be converted to a string. the basic to_s implementation just
outputs the value of the text field. but i want to also store text fields
that contain textile markup, or eruby markup or whatever, in other words i
want to associate a type with each text field and i want the to_s method to
behave differently depending on the type. but for to_s to be able to run it
needs to get the value of the text field from C.

thanks
konstantin
 
K

konsu

hello,

let me rephrase the question, may be this will get more responses:

is it a bad programming style in ruby to use 'super' in a module definition?
that is, is it bad to invoke methods that will be defined in the object that
will mix in this module?

thanks
konstantin
 
M

Malte Milatz

konsu said:
class C defines an instance variable @x:

class C
def initialize; @x = 'C'; end
def to_s; @x; end
end

i want to override 'to_s' method. but the new implementation that i have
in my fragment still needs to refer to @x.

I don't see a (stylistic or whatever) problem in directly referring to @x
from the module. Since it works, it must be supposed to be done like this,
I guess.

Malte
 
D

David Vallner

D=C5=88a Streda 08 Febru=C3=A1r 2006 19:38 konsu nap=C3=ADsal:
is it a bad programming style in ruby to use 'super' in a module
definition? that is, is it bad to invoke methods that will be defined in
the object that will mix in this module?
Actually, I'll go a bit further than Jacob and say that is -exactly- what=20
mixins are supposed to do - enhance the functionality by providing some ext=
ra=20
methods that build on existing class functionality. Mixins that are=20
completely independent from the class would best be separated into=20
stand-alone classes in the most cases.

As far as instance variables go, I tend to avoid making stateful mixins=20
myself, and prefer using accessor methods instead of directly accessing the=
=20
including class. I could go on about decoupling logic in here, but it prett=
y=20
much boils down to a matter of taste / religion.

David Vallner
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top