Hooking Class Methods

This works:

  module PromoteSelf
    def method_added( *args )
      p args
    end
  end

  module String::Format
    extend PromoteSelf

    def jumble( obj, x, y )
      p obj+x+y
    end
  end

But NOT this:

  module PromoteSelf
    def method_added( *args )
      p args
    end
  end

  module String::Format
    class << self
      extend PromoteSelf

      def jumble( obj, x, y )
        p obj+x+y
      end
    end
  end

How do I hook in module/class method creation?

Thanks,
T.

To answer my own question:

  module PromoteSelf
    def singleton_method_added( *args )
      p args
    end
  end

module String::Format
    class << self
      include PromoteSelf

      def jumble( obj, x, y )
        p obj+x+y
      end
    end
  end

But I'll be honest with you. I find it odd. Why would'nt the later
work? It almost seems like it would have to be purposfully disallowed.

Nonetheless, now that I've got this working I have something neat to
show ...

Trans wrote:

To answer my own question:

  module PromoteSelf
    def singleton_method_added( *args )
      p args
    end
  end

module String::Format
    class << self
      include PromoteSelf

      def jumble( obj, x, y )
        p obj+x+y
      end
    end
  end

But I'll be honest with you. I find it odd. Why would'nt the later
work? It almost seems like it would have to be purposfully disallowed.

Module#method_added is called when a new method is added to the
module's collection of methods. When you define a method as
SomeModule.method_name, you're not adding a method to the module's
collection, you're adding a singleton method to the SomeModule object.
You could do the same for any object, which is why Object contains
singleton_method_added.

Trans schrieb:

To answer my own question:

  module PromoteSelf
    def singleton_method_added( *args )
      p args
    end
  end

module String::Format
    class << self
      include PromoteSelf

      def jumble( obj, x, y )
        p obj+x+y
      end
    end
  end

But I'll be honest with you. I find it odd. Why would'nt the later
work? It almost seems like it would have to be purposfully disallowed.

Nonetheless, now that I've got this working I have something neat to
show ...

Compare [ruby-core:4855]. Matz thinks this would make singleton classes more present in Ruby.

Regards,
Pit

Charles Steinman wrote:

Module#method_added is called when a new method is added to the
module's collection of methods. When you define a method as
SomeModule.method_name, you're not adding a method to the module's
collection, you're adding a singleton method to the SomeModule object.
You could do the same for any object, which is why Object contains
singleton_method_added.

Thanks. What I found strange was that

  def self.method_added

is a singleton method too. So I thought it would hook singleton method
creation. I see now that this isn't the case b/c singletons "shadow"
the real methods. So this wouldn't work. Hence the seperate method.

T.

Pit Capitain wrote:

Compare [ruby-core:4855]. Matz thinks this would make singleton classes
more present in Ruby.

Intersting, thanks. Erm... how much more "present" can you get when
every class method is one? Besides what's so bad about singletons? Hek,
I think they should be layerable.

T.

Trans schrieb:

Pit Capitain wrote:

Compare [ruby-core:4855]. Matz thinks this would make singleton classes
more present in Ruby.

Intersting, thanks. Erm... how much more "present" can you get when
every class method is one? Besides what's so bad about singletons? Hek,
I think they should be layerable.

As I understand it, Matz wanted to have singleton methods in Ruby, but not necessarily singleton classes. He has seen singleton classes as a feature of the current implementation, not as part of the "official" language. IIRC, this could change in the future. I'm waiting...

Regards,
Pit