Ilmari Heikkinen
1/29/2005 6:41:00 PM
Hallo,
On 29.1.2005, at 19:41, itsme213 wrote:
> I think I want to use this design in a few places in my code, to write
> a
> single module that can be adapted to different needs:
>
> module M
> <normal stuff>
> def self.[](params)
> create new module
> populate using params and (M or <normal stuff>)
> return new module
> end
> end
>
> class A
> include M[p1, p2]
> end
>
> Is this reasonable Ruby style? Have others found uses for this?
Don't know much about Ruby style, but I'll open my trap anyhow.
Less code you have to write, less code you have to change.
Though there are some pitfalls in this technique.
Marshalling might not work, code might become harder to read,
documentation tools might not work.
Overall.. *shrug*
dunno
If it makes the code easier to maintain, go for it.
If it makes the code more readable, go for it.
If it makes you happy, go for it.
I used run-time generated modules to implement a mimetype
hierarchy with multiple inheritance (text/html is subclass of
text/plain, text, and application/octet-stream).
So that it was possible to do
Mime['text/html'].is_a? Mime['application/octet-stream'] #=> true
Mime['text/html'].is_a? Mime['text/plain'] #=> true
etc.
> If the generated module did not include M, what would be a good way of
> keeping the instantiated M[param] in sync with changes in M? e.g. new
> methods are added to M, or an M method is re-defined?
One would usually use include for that, yes. If you don't want the
generated
module to be related to M, it shouldn't care about changes to M anyhow,
no?
------------------------------------------------------------------------
----------------
DASH 100M GO!!