On Wed, Mar 07, 2007 at 06:40:20PM +0900, Brian Candler wrote:
> Final point. If the requirement is that 'meta-programming' methods should be
> available always on all objects, then I can offer a completely different
> solution:
> 
>     # was: foo.class
>     Object.class_of(foo)
> 
>     # was: foo.send(meth,*args)
>     Object.send_to(foo, meth, *args)
> 
>     # was: foo.method(:bar)
>     Object.method_of(foo, :bar)
> 
> That is, by making all these operations singleton methods of a module,
> instead of (or as well as) instance methods which are present on all
> objects, then there is never any chance of them being overriden by the
> object itself. It's not very OO-like, but it *is* immune from this problem.

And this is straightforward to implement:

----- 8< -------------------------------------------------------
module Meta
  def self.send(obj, meth, *args)
    Object.instance_method(:send).bind(obj).call(meth, *args)
  end

  def self.class(obj)
    Object.instance_method(:class).bind(obj).call
  end

  # etc
end

class Foo
  def send(*args)
    "whoo!"
  end
  def class
    "stupid!"
  end
end

f = Foo.new

puts f.class                     # stupid!
puts Meta.class(f).inspect       # Foo

puts f.send(:object_id)          # whoo!
puts Meta.send(f, :object_id)    # -605608944
----- 8< -------------------------------------------------------

Implement all the methods you consider to be "metaprogramming" with whatever
names you think are appropriate. Stick it up as a module on rubyforge, in
case anyone comes across the same need as you. Job done, and no need to
change the core language :-)

Regards,

Brian.