From: Trans Date: 2006-12-06T14:02:22+09:00 Subject: Re: Method Namespace Pit Capitain wrote: > Tom, could you try this version? > > def namespace( mod, &blk ) > > # If block is given then create a module, otherwise > # get the name of the module. > if block_given? > name = mod.to_s > mod = Module.new(&blk) > else > name = mod.basename.downcase > mod = mod.dup > end > > # Include the module. This is neccessary, otherwise > # Ruby won't let us bind the instance methods. > include mod > > # Save the instance methods of the module and > # replace them with a "transparent" version. > methods = {} > mod.instance_methods(false).each do |m| > methods[ m.to_sym ] = mod.instance_method(m) > mod.instance_eval do > define_method(m) do > super > end > end > end > > # Add a method for the namespace that delegates > # via the Functor to the saved instance methods. > define_method(name) do > Functor.new(methods) do |op, mtab, *args| > mtab[op].bind(self).call(*args) > end > end > end Bloody hek, that's a clever solution. Works like a charm. Pit Captain, you never cease to amaze! I'm putting this in Facets to replace the weak SimpleDelegtor I had been using. Thanks! T.