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

Discussion in 'Ruby' started by Trans, Jul 29, 2005.

  1. Trans

    Trans Guest

    .... 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.
     
    Trans, Jul 29, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    1,919
    Mike Treseler
    Feb 10, 2005
  2. isore
    Replies:
    0
    Views:
    541
    isore
    Jul 1, 2004
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    959
    Andreas Bogenberger
    Feb 22, 2008
  4. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    118
    Brian
    Apr 8, 2005
  5. Replies:
    2
    Views:
    161
    Dave Angel
    Mar 8, 2013
Loading...

Share This Page