I am a newbie on a journey to learn Ruby and Rails.
My toy project has a class hierarchy. All subclasses of a certain class have a method with this structure:
def mymethod
code_A_common_to_subclasses
subclass_specific_code
code_B_common_to_subclasses
end
Normally, this would be an excellent case for yield:
def mymethod
code A
yield
code B
where the subclass-specific code would be in a block provided to mymethod.
Since several subclasses use mymethod, it should be implemented in the superclass. However, my head spins trying to understand how a subclass can provide its subclass_specific_code block to the superclass method.
Does this make sense at all? Can this be done as outlined? How would you implement such a pattern?