title: How do I wrap the invocation of a Ruby method by including a module?
body: <p>I want to be notified when certain things happen in some of my classes. I want to set this up in such a way that the implementation of my methods in those classes doesn't change.</p>
  
  <p>I was thinking I'd have something like the following module:</p>
  
  <pre><code>module Notifications
    extend ActiveSupport::Concern
  
    module ClassMethods
      def notify_when(method)
        puts "the #{method} method was called!"
        # additional suitable notification code
        # now, run the method indicated by the `method` argument
      end
    end
  end
  </code></pre>
  
  <p>Then I can mix it into my classes like so:</p>
  
  <pre><code>class Foo
    include Notifications
  
    # notify that we're running :bar, then run bar
    notify_when :bar
  
    def bar(...)  # bar may have any arbitrary signature
      # ...
    end
  end
  </code></pre>
  
  <p>My key desire is that I don't want to have to modify <code>:bar</code> to get notifications working correctly. Can this be done? If so, how would I write the <code>notify_when</code> implementation?</p>
  
  <p>Also, I'm using Rails 3, so if there are ActiveSupport or other techniques I can use, please feel free to share. (I looked at <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/notifications.rb" rel="noreferrer">ActiveSupport::Notifications</a>, but that would require me to modify the <code>bar</code> method.)</p>
  
  <hr>
  
  <p>It has come to my attention that I might want to use "the Module+super trick". I'm not sure what this is -- perhaps someone can enlighten me?</p>