ruby - Is overriding a module's method a good convention? -
i've got kind of template patterned
module few method defined (default behaviour) , method below:
def tax 1.2 end def do_something! raise "please implement in class" end
i've read in cases should use modules on inheritance because of inheritance capabilities (single inheritance) , when don't need super()
@ all.
but feel bit guilty override raise "..."
methods , default (like tax
method), because module.
what think?
when need override methods should rather use inheritance or including modules?
the rule follow is: when method has defined in class including module (e.g. module acts interface) do:
def method_that_needs_to_be_defined raise nomethoderror end
it's practice, prevents unexpected calls yet undefined method.
example:
module speaker def speak raise nomethoderror end end class bird < animal include speaker def speak 'chirp' end end
Comments
Post a Comment