title: How do I run code before and after a method in a sub class?
body: <p>My first thoughts are some thing like this:</p>
  
  <pre><code>class AbstractBuilder
    attr_reader :time_taken
  
    def build_with_timer
      started_at = Time.now
      build
      @time_taken = Time.now - started_at
    end
  
    def build
      raise 'Implement this method in a subclass' 
    end
  end
  
  class MyBuilder &lt; AbstractBuilder
    def build
      sleep(5)
    end
  end
  
  builder = MyBuilder.new.build_with_timer
  puts builder.time_taken
  </code></pre>
  
  <p>I would suspect there is a better way which offers better flexibility, for example ideally I'd like to call 'build' on an instance of MyBuilder instead of 'build_with_timer' and always have the execution time recorded.</p>
  
  <p>I did consider using alias_method from initialize or even using a module mixin instead of class inheritance which would override the build method calling super in the middle (not sure if that would work). Before I go down the rabbit hole I thought I'd see if there is an established practice. </p>