From: benjohn@... Date: 2006-10-03T16:22:59+09:00 Subject: Re: Creating modules > On Mon, Oct 02, 2006 at 05:45:52PM +0900, benjohn@fysh.org wrote: >> >> I am dynamically creating modules [1]. I would like to be able to add >> methods and constants to the module using the "normal" notation: >> >> module MyModule >> def a_method; 'wibble'; end >> end >> >> But the module doesn't have an constant associated with it, so I'm >> getting an error from... >> >> class MyModuleClass < Module >> def initialize(a_constant_to_go_in_module) >> module self >> K = a_constant_to_go_in_module >> def a_method; end >> ... >> end >> end >> end >> >> Is there a way to do this, or do I have to start using the >> define_method >> and const_set methods? >> > No, well at least not if you know the method name before hand. I presume you meant "... not if you _don't_ know the method name ..."? > def initialize(a_constant_to_go_in_module) > self.module_eval do > K = a_constant_to_go_in_module > def a_method; 'wibble'; end > end > end Ah! I think that's exactly what I want :) Thanks, I'll give it a try :)