some metaprogramming with define_method

P

Philipp Hofmann

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
 
M

Mikael Høilund

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}" }
Here you're in class scope; self points to ProxyTest. You in essense
defined Class.test_method. Remove ".class", and it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top