From: Robert Klemme Date: 2005-09-14T22:01:34+09:00 Subject: Re: extend question Sebastian Pad� wrote: > Hello everybody, > > I apologize if this a stupid question - but I'd be very grateful for > any hints! > > Short Problem > ------------- > Is there a way of _forcing_ methods to be > overwritten when including a module? > > Background > ---------- > I'm implementing some algorithm, let's call it M, as a class. Now M > needs to be customised - e.g. different bits need to be > instatiated. If there was just one such bit, I'd create subclasses > that inherit from M. > > However, there are several such bits -- and my idea was to realise > this by writing, for each such customisation point, different Modules > which all offer the same "plugin" method. As an example, look at the > code below. > So, is there any way to "force" the methods to be overwritten, even if > it appears to be superfluous? Or am I using an unsuitable approach to > begin with? Any In your case no effort is necessary since you just extend with one of your modules and Ruby will complain as soon as the needed methods are invoked. If you want to check in customize, you can use ModuleA.instance_methods to check whether all methods are there. An additional caveat: if you invoke customize multiple times you might run into trouble if you need to extend the same instance with different modules over time. An approach that uses delegation (see state pattern, strategy pattern and related patterns) might be better in that case. Kind regards robert