Method_added for class methods

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.

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.

s/singletin/singleton/

Hi --

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

s/tin/ton/ :slight_smile:

     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? :slight_smile:

David

···

On Sat, 10 Sep 2005, Trans wrote:

--
David A. Black
dblack@wobblini.net

   def singletin_method_added(*args)

s/tin/ton/ :slight_smile:

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? :slight_smile:

Indeed :slight_smile:

Hi --

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

s/tin/ton/ :slight_smile:

     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? :slight_smile:

Yes!

Kind regards

    robert

···

David A. Black <dblack@wobblini.net> wrote:

On Sat, 10 Sep 2005, Trans wrote:

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)

Hi --

Hi --

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

s/tin/ton/ :slight_smile:

     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

···

On Sat, 10 Sep 2005, Robert Klemme wrote:

David A. Black <dblack@wobblini.net> wrote:

On Sat, 10 Sep 2005, Trans wrote:

--
David A. Black
dblack@wobblini.net

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.

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?