On-the-fly convertion of function modules into include modules

T

Trans

.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top