[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

some metaprogramming with define_method

Philipp Hofmann

4/24/2008 9:26:00 PM

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


1 Answer

Mikael Høilund

4/24/2008 9:35:00 PM

0


On Apr 24, 2008, at 23:26, Philipp Hofmann wrote:

> 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.
>
> end
>
> end
>
> end
>
> class ProxyTest
>
> include Proxy
>
> proxy_method :test_method
>
> end
>
> proxy = ProxyTest.new
> proxy.test_method
>
>
> Any hints greatly appreciated.
> g phil
>
>