S
Sebastian Padó
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.
class M
def customize(param)
if param == "useA" then
extend(ModuleA)
elsif param == "useB" then
extend(ModuleB)
else
raise someException
end
end
def runAlgorithm
pluginMethod()
end
end
module A
def pluginMethod
puts "module A"
end
end
module B
def pluginMethod
puts "module B"
end
end
But what happens if I customize the class several times?
m_obj = new M()
m_obj.runAlgorithm() -> fails; that it fine
First I run it extended with A...
m_obj.customize("useA")
m_obj.runAlgorithm() -> prints "module A"
Then extended with B...
m_obj.customize("useB")
m_obj.runAlgorithm() -> prints "module B"
But if I want to switch back to A, it goes wrong:
m_obj.customize("useA")
m_obj.runAlgorithm() -> prints "module B"
Somewhere in the documentation of Module I found that
Module.append_features only appends its features if they have not been
added already - I suspect that this is the underlying reason.
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
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.
class M
def customize(param)
if param == "useA" then
extend(ModuleA)
elsif param == "useB" then
extend(ModuleB)
else
raise someException
end
end
def runAlgorithm
pluginMethod()
end
end
module A
def pluginMethod
puts "module A"
end
end
module B
def pluginMethod
puts "module B"
end
end
But what happens if I customize the class several times?
m_obj = new M()
m_obj.runAlgorithm() -> fails; that it fine
First I run it extended with A...
m_obj.customize("useA")
m_obj.runAlgorithm() -> prints "module A"
Then extended with B...
m_obj.customize("useB")
m_obj.runAlgorithm() -> prints "module B"
But if I want to switch back to A, it goes wrong:
m_obj.customize("useA")
m_obj.runAlgorithm() -> prints "module B"
Somewhere in the documentation of Module I found that
Module.append_features only appends its features if they have not been
added already - I suspect that this is the underlying reason.
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