title: Ruby method interception
body: <p>I want to intercept method calls on a ruby-class and being able to do something before and after the actual execution of the method. I tried the following code, but get the error:</p>
  
  <blockquote>
    <p>MethodInterception.rb:16:in <code>before_filter': (eval):2:in</code>alias_method': undefined method
    <code>say_hello' for class</code>HomeWork'
    (NameError)
            from (eval):2:in `before_filter'</p>
  </blockquote>
  
  <p>Can anybody help me to do it right?</p>
  
  <pre><code>class MethodInterception
  
    def self.before_filter(method)
      puts "before filter called"
      method = method.to_s
      eval_string = "
        alias_method :old_#{method}, :#{method}
  
        def #{method}(*args)
          puts 'going to call former method'
          old_#{method}(*args)
          puts 'former method called'
        end
      "
      puts "going to call #{eval_string}"
      eval(eval_string)
      puts "return"
    end
  end
  
  class HomeWork &lt; MethodInterception
    before_filter(:say_hello)
  
    def say_hello
      puts "say hello"
    end
  
  end
  </code></pre>