Why is this cause an infinite loop?

Discussion in 'Ruby' started by Trans, Jul 17, 2008.

  1. Trans

    Trans Guest

    Why is this cause an infinite loop?

    class X
    def m; "m"; end
    end

    class Y < X
    def self.method_added(sym)
    alias_method("__#{sym}", sym)
    remove_method(sym)
    end
    end

    class Y
    def m; m; end
    end

    Y.new.__m

    T.
     
    Trans, Jul 17, 2008
    #1
    1. Advertising

  2. Trans

    Pit Capitain Guest

    2008/7/17 Trans <>:
    > Why is this cause an infinite loop?
    >
    > class X
    > def m; "m"; end
    > end
    >
    > class Y < X
    > def self.method_added(sym)


    p sym

    > alias_method("__#{sym}", sym)
    > remove_method(sym)
    > end
    > end
    >
    > class Y
    > def m; m; end
    > end
    >
    > Y.new.__m


    Tom, see above. Creating an alias triggers method_added.

    Regards,
    Pit
     
    Pit Capitain, Jul 17, 2008
    #2
    1. Advertising

  3. Trans

    Trans Guest

    On Jul 17, 10:55=A0am, "Pit Capitain" <> wrote:
    > 2008/7/17 Trans <>:
    >
    > > Why is this cause an infinite loop?

    >
    > > =A0class X
    > > =A0 =A0def m; "m"; end
    > > =A0end

    >
    > > =A0class Y < X
    > > =A0 =A0def self.method_added(sym)

    >
    > =A0 =A0 =A0 =A0p sym
    >
    > > =A0 =A0 =A0alias_method("__#{sym}", sym)
    > > =A0 =A0 =A0remove_method(sym)
    > > =A0 =A0end
    > > =A0end

    >
    > > =A0class Y
    > > =A0 =A0def m; m; end
    > > =A0end

    >
    > > =A0Y.new.__m

    >
    > Tom, see above. Creating an alias triggers method_added.


    Dooh!

    Thanks, Pit.

    T.
     
    Trans, Jul 17, 2008
    #3
  4. Trans

    Robert Dober Guest

    On Thu, Jul 17, 2008 at 7:23 PM, Trans <> wrote:
    >
    >
    > On Jul 17, 10:55 am, "Pit Capitain" <> wrote:
    >> 2008/7/17 Trans <>:
    >>
    >> > class Y < X
    >> > def self.method_added(sym)

    return if /^__/ === sym.to_s

    But you probably figured that out by yourself ;).

    Cheers
    Robert


    --
    http://ruby-smalltalk.blogspot.com/

    ---
    AALST (n.) One who changes his name to be further to the front
    D.Adams; The Meaning of LIFF
     
    Robert Dober, Jul 17, 2008
    #4
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  2. Stu
    Replies:
    11
    Views:
    818
    Mark Hubbart
    Nov 13, 2004
  3. Trans
    Replies:
    3
    Views:
    162
  4. Bartosz Wegrzyn

    Onblur() event cause infinite loop

    Bartosz Wegrzyn, Sep 7, 2003, in forum: Javascript
    Replies:
    2
    Views:
    153
    Bartosz Wegrzyn
    Sep 8, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    405
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page