# Some metaprogramming with define\_method

**URL:** <https://www.ruby-forum.com/t/some-metaprogramming-with-define-method/135786>\
**Category:** Ruby\
**Created:** [April 24, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/some-metaprogramming-with-define-method/135786 "2008-04-24T23:29:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [April 24, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/some-metaprogramming-with-define-method/135786/1 "2008-04-24T23:29:49Z")

</div>

Hello,

I was hoping that the following snippet would output

test\_method

instead it raises an ‘undefined method’.

module Proxy

```
def self.append_features(mod)
  mod.extend(ClassMethods)
end

module ClassMethods

  def proxy_method(name)
    self.class.send(:define_method, name) { puts "called #{name}" }
  end

end

```

end

class ProxyTest

```
include Proxy

proxy_method :test_method

```

end

proxy = ProxyTest.new  
proxy.test\_method

Any hints greatly appreciated.  
g phil

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [April 24, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/some-metaprogramming-with-define-method/135786/2 "2008-04-24T23:35:42Z")

</div>

On Apr 24, 2008, at 23:26, Philipp H. wrote:

> def self.append\_features(mod)  
> mod.extend(ClassMethods)  
> end
> 
> module ClassMethods
> 
> ```
> def proxy_method(name)
> self.class.send(:define_method, name) { puts "called #{name}" }
> 
> ```

Here you’re in class scope; self points to ProxyTest. You in essense  
defined Class.test\_method. Remove “.class”, and it works.
