Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   On-the-fly convertion of function modules into include modules (http://www.velocityreviews.com/forums/t823270-on-the-fly-convertion-of-function-modules-into-include-modules.html)

Trans 07-29-2005 02:08 PM

On-the-fly convertion of function modules into include modules
 
.... I've been wanting this functionality for years. I even considered
writing an RCR for it. Now I finally figured out a way to do it in
Ruby. And it was surprisingly simple!

module PromoteSelf
def singleton_method_added( meth)
d = %{
def #{meth}(*args)
#{self.name}.#{meth}(self,*args)
end
}
self.class_eval d
end
end

module String::Format
class << self
include PromoteSelf

def jumble( obj, x, y )
p obj+x+y
end
end
end

String::Format.jumble( "TryMe", '!', '!' )

class String
include Format
end

"TryMe".jumble( '!', '!')

Now I _can_ have my cake and eat it too :-)

T.



All times are GMT. The time now is 07:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.