I’m working on a plugin system for a IRC client I’m currently making.
The current plan is for plugins to extend the base classes of the
program. Often a plugin will need to add a little bit of functionality
to an already existing method in an already existing class.
For example, I’ve got a logging plugin. I want it to extend the Channel
class to allow for logging of conversations. It needs to redefine
Channel’s initialize method to open a log file. But it can’t eliminate
all the important things the base initialize method did.
I’ve thought about doing this with alias, but I haven’t succeeded. My
thoughts were to rename ‘initialize’ to ‘old_initialize’, then have the
plugin redefine ‘initialize’. In code:
alias old_initialize initialize
# Then write custom code. (To open a log file, for example.)
This doesn’t work, however, because it leads to a stack overflow. I
think the reason for the overflow is because, when this is done twice,
old_initialize will call itself recursively with no end.
Anyone have any ideas for how this could be done? It doesn’t have to use
alias, but the general intention is thus: I want to redefine a method
and ‘copy and paste’ the old code from the method into the new method at
a given point. Sort of like using ‘super’, but no inheritence required.
Thanks! Responses are greatly appreciated.