On Fri, 14 Oct 2005, Jeff Wood wrote:

> I think I've got a very challenging project on my hands but here's I'm
> trying to create.
>
> I want a completely clean(transparent) proxy object.
>
> One where ALL methods simply return the result of the same call on the
> proxied ( not the proxy ) object.
>
> I mean, if the user wants to add/override methods, that's fine, but by
> default, EVERY call should be passed through.
>
> I hope that makes sense...
>
> So:
>
> a = String
> b = Proxy.new( a )
>
> # should be the same
> a.class
> b.class
>
> # should be the same
> a.methods
> b.methods
>
> I want it be completely transparent.
>
> Is that possible?

this'll be a good start:

   harp:~ > cat a.rb
   class Proxy
     ignore = %w( __send__ __id__ )

     instance_methods.each do |m|
       next if ignore.include? m
       module_eval <<-code
         def #{ m } *a, &b
           __obj.send "#{ m }", *a, &b
         end
       code
     end

     attr_accessor '__obj'

     def initialize obj
       self.__obj = obj
     end
   end

   a = String::new '42'
   b = Proxy::new a

   p a.class
   p b.class

   p(a.methods == b.methods)



   harp:~ > ruby a.rb
   String
   String
   true


hth.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze.  --Nagarjuna
===============================================================================