- class C that implements some arbitrary methods
- a database with code fragments stored as text
i create a variable ‘var’ of class C and then read some code fragments
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
… . .
so if a fragment implements some methods that are already defined in C
mixing in effectively replaces the original methods with the ones
class C defines an instance variable @x:
def initialize; @x = ‘C’; end
def to_s; @x; end
i want to override ‘to_s’ method. but the new implementation that i have
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
happens to be mixed in to. can anyone comment on this? basically the
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
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
that contain textile markup, or eruby markup or whatever, in other words
want to associate a type with each text field and i want the to_s method
behave differently depending on the type. but for to_s to be able to run
needs to get the value of the text field from C.