method_added for class methods

Discussion in 'Ruby' started by Caio Chassot, Sep 9, 2005.

  1. Caio Chassot

    Caio Chassot Guest

    Hi all,

    Is there any way to get method_added to work with class methods? (or to
    get the same functionality trough another means?)

    Any pointers are appreciated.
    Caio Chassot, Sep 9, 2005
    #1
    1. Advertising

  2. Caio Chassot

    Trans Guest

    class X
    class << self
    def method_added(*args)
    p args
    end
    def singletin_method_added(*args)
    p args
    end
    end
    end

    Don't ask me to explain it --it's mind numbing.

    T.
    Trans, Sep 9, 2005
    #2
    1. Advertising

  3. Caio Chassot

    Trans Guest

    s/singletin/singleton/
    Trans, Sep 9, 2005
    #3
  4. Hi --

    On Sat, 10 Sep 2005, Trans wrote:

    > class X
    > class << self
    > def method_added(*args)
    > p args
    > end
    > def singletin_method_added(*args)


    s/tin/ton/ :)

    > p args
    > end
    > end
    > end


    I believe that only the singleton_method_added actually does anything.
    I guess that's because the only methods you can add to this class are
    singletons, and that gets priority.

    At least, when I tried method_added, I couldn't get any output.

    > Don't ask me to explain it --it's mind numbing.


    Like so many things, it comes down to:

    * objects can have singleton methods
    * singleton methods are stored in the object's singleton class
    * classes are objects

    It's so lovely, don't you think? :)


    David

    --
    David A. Black
    David A. Black, Sep 9, 2005
    #4
  5. Caio Chassot

    Caio Chassot Guest

    >> def singletin_method_added(*args)
    >
    > s/tin/ton/ :)
    >


    Thanks. That did the trick. I didn't know about singleton_method_added.


    > Like so many things, it comes down to:
    >
    > * objects can have singleton methods
    > * singleton methods are stored in the object's singleton class
    > * classes are objects
    >
    > It's so lovely, don't you think? :)
    >


    Indeed :)
    Caio Chassot, Sep 9, 2005
    #5
  6. Caio Chassot

    Caio Chassot Guest

    >
    > Thanks. That did the trick. I didn't know about singleton_method_added.
    >


    So I just bought the pickaxe 2 and it's there. Gotta love this book.

    I was surprised I didn't find it in ri, but now I see it there and it's
    a method of Object, not Module or Class. (which actually makes sense,
    when you think about it for a second)
    Caio Chassot, Sep 9, 2005
    #6
  7. David A. Black <> wrote:
    > Hi --
    >
    > On Sat, 10 Sep 2005, Trans wrote:
    >
    >> class X
    >> class << self
    >> def method_added(*args)
    >> p args
    >> end
    >> def singletin_method_added(*args)

    >
    > s/tin/ton/ :)
    >
    >> p args
    >> end
    >> end
    >> end

    >
    > I believe that only the singleton_method_added actually does anything.
    > I guess that's because the only methods you can add to this class are
    > singletons, and that gets priority.
    >
    > At least, when I tried method_added, I couldn't get any output.


    Worksforme:

    >> class Bar
    >> class <<self; def method_added(m) p m end end
    >> def x() end
    >> end

    :x
    => nil
    >>


    >> Don't ask me to explain it --it's mind numbing.

    >
    > Like so many things, it comes down to:
    >
    > * objects can have singleton methods
    > * singleton methods are stored in the object's singleton class
    > * classes are objects
    >
    > It's so lovely, don't you think? :)


    Yes!

    Kind regards

    robert
    Robert Klemme, Sep 10, 2005
    #7
  8. Hi --

    On Sat, 10 Sep 2005, Robert Klemme wrote:

    > David A. Black <> wrote:
    >> Hi --
    >>
    >> On Sat, 10 Sep 2005, Trans wrote:
    >>
    >>> class X
    >>> class << self
    >>> def method_added(*args)
    >>> p args
    >>> end
    >>> def singletin_method_added(*args)

    >>
    >> s/tin/ton/ :)
    >>
    >>> p args
    >>> end
    >>> end
    >>> end

    >>
    >> I believe that only the singleton_method_added actually does anything.
    >> I guess that's because the only methods you can add to this class are
    >> singletons, and that gets priority.
    >>
    >> At least, when I tried method_added, I couldn't get any output.

    >
    > Worksforme:
    >
    >>> class Bar
    >>> class <<self; def method_added(m) p m end end
    >>> def x() end
    >>> end

    > :x
    > => nil


    Right -- actually the one that I expected would work, but didn't,
    was a different one, and I got my wires crossed. Here it is:

    class C
    class << self
    def self.method_added(m)
    p m
    end
    def x
    end
    end
    end


    David

    --
    David A. Black
    David A. Black, Sep 10, 2005
    #8
  9. Caio Chassot

    Caio Chassot Guest

    > Right -- actually the one that I expected would work, but didn't,
    > was a different one, and I got my wires crossed. Here it is:
    >
    > class C
    > class << self
    > def self.method_added(m)
    > p m
    > end
    > def x
    > end
    > end
    > end
    >


    That's what I'd been trying before I posted.

    Can anyone explain why it doesn't work?
    Caio Chassot, Sep 10, 2005
    #9
  10. Caio Chassot

    Caio Chassot Guest

    >
    > Worksforme:
    >
    >>> class Bar
    >>> class <<self; def method_added(m) p m end end
    >>> def x() end
    >>> end

    > :x
    > => nil


    Well, but the question was about class methods, and you're defining an
    instance method.
    Caio Chassot, Sep 10, 2005
    #10
    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. Jim Cain
    Replies:
    0
    Views:
    85
    Jim Cain
    Jul 18, 2003
  2. robertj
    Replies:
    2
    Views:
    70
    Trans
    Dec 12, 2005
  3. Keith Rarick

    method_added hook and class methods

    Keith Rarick, Aug 3, 2007, in forum: Ruby
    Replies:
    4
    Views:
    120
    Robert Dober
    Aug 4, 2007
  4. Trans
    Replies:
    7
    Views:
    131
    Trans
    Nov 14, 2007
  5. Kenneth McDonald
    Replies:
    5
    Views:
    300
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page