On Sat, 15 May 2004 02:53:52 +0900, gabriele renzi 
<surrender_it / rc1.vip.ukl.yahoo.com> wrote:

> remove namespace and name it module.
> Than make class declaration semantic differnt so that it is like:
>
> module M
>  class String < ::String
>  end
> end
>
> Now add the magic that include M should merge M::String and String.
> Then post anrcr and I'll vote for it :))
>

It's diffcult to define the merging.
There should be some interceptor, who catches missing methods
and lookup in the callers binding for the extension.

I assume there is Kernel#caller_binding
(somethimg similar was posted recently as RCR).


class String
  alias _method_missing method_missing
  def method_missing(meth, *args, &block)
   s = <<-EOF
     names = self.class.name.split('::')
     mod = Kernel
     names.map do |name|
      mod = mod.const_get(name)
      mod.const_get(:String) and mod
     end.compact
   EOF
   exts = eval(s, caller_binding)   # collecting the extension modules
   exts.each {|ext| extend ext }    # extending with my own modules
   if respond_to? meth
    send(meth, *args, &block)       # meth was found in extension
   else
    _method_missing(meth, *args, &block) # meth is still missing
   end
  end

end

module A
  module String
   def to_yaml
     ..
   end
  end

  class X
   def initialize(s)
    @s = s.to_yaml          # I need the binding from here !!!!
    p s.singleton_methods   # => ["to_yaml"]
   end
  end
end

A::X.new "astring"


Every missing method for a normal String object will be intercepted,
and the object gets extended with a module in the calling environment.
That's actually what we needs.
My own extensions are only used in my modules.
The code above is only a demonstration of the algorithm.
There is a problem left, I cannot pass the strings outside
my module, because other people are expecting the standard
String interface, so the whole thing should be integrated in the
language core without singleton classes, extending only for the
actual method call.


-- 
Matthias Georgi
matti_g / gmx.de