Hi -- On Mon, 3 Oct 2005, Devin Mullins wrote: > Rob Rypka wrote: > >> do you have an example of a task for which you want to use >> metaclasses? >> > What if you're trying to build a <a > href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431.22">factory > factory factory</a>? > >> Hmmm. Here is what I want: >> >> module M >> # magic for #foo, #bar >> end >> >> class C >> include M >> # should do the equivalent of: >> # def C.foo(n); @foo = n; end >> # def bar; self.class.iv_get "@foo"; end >> foo 55 # >> end >> >> C.new.bar #=> 55 >> > You gotta do a little extra work for that -- a module gets included as > instance methods if you use 'include', or class methods if you use 'extend' > (due to that singleton mojo). Override that: > > module M > def self.included(mod) > mod.extend ClassMethods > end > def bar; self.class.instance_variable_get "@foo" end > module ClassMethods > def foo(n) @foo = n end > end > end > > class C > include M > foo 55 > end > > p C.new.bar You can just define the method directly on the object -- you don't need that second module: module M def self.included(mod) def mod.foo(n) @foo = n end end etc. David -- David A. Black dblack / wobblini.net