How to make proxy objects

looking for some tips on the best way to create a transparent proxy
object, i.e. an object to act as a go between. i wish ruby had such
things built in. (acutally i think it does but they aren’t exposed in
the api) currently this is what i have as the bases, to get it
transparent i remove as many of the object’s methods as possible and use
method_missing to then deal with the passing off of methods:

class Module
public :undef_method
end

class Proxy_Object
def initialize(peer, oid)
methods.each { |mid|
if not [‘inspect’,
‘instance_eval’,
‘instance_variables’,
id’,
‘id’,
‘new’,
‘methods’,
‘method_missing’,
‘undef_method’,
‘class’,
send’,
‘send’].include?(mid)
self.class.undef_method(mid.intern)
end
}
end

def method_missing(mid, *args)
  ...act as proxy...
end

end

···


tom sawyer, aka transami
transami@transami.net

oops…ignore the ‘(peer, oid)’ that’s what i’m using to know what
object is being proxied for, but for this its not important. there can
be otherways i imagine.

···

On Wed, 2002-09-11 at 04:27, Tom Sawyer wrote:

looking for some tips on the best way to create a transparent proxy
object, i.e. an object to act as a go between. i wish ruby had such
things built in. (acutally i think it does but they aren’t exposed in
the api) currently this is what i have as the bases, to get it
transparent i remove as many of the object’s methods as possible and use
method_missing to then deal with the passing off of methods:

class Module
public :undef_method
end

class Proxy_Object
def initialize(peer, oid)
methods.each { |mid|
if not [‘inspect’,
‘instance_eval’,
‘instance_variables’,
id’,
‘id’,
‘new’,
‘methods’,
‘method_missing’,
‘undef_method’,
‘class’,
send’,
‘send’].include?(mid)
self.class.undef_method(mid.intern)
end
}
end

def method_missing(mid, *args)
  ...act as proxy...
end

end


tom sawyer, aka transami
transami@transami.net


tom sawyer, aka transami
transami@transami.net

class Module
public :undef_method
end

class Proxy_Object
def initialize(peer, oid)
methods.each { |mid|
if not [‘inspect’,
‘instance_eval’,
‘instance_variables’,
id’,
‘id’,
‘new’,
‘methods’,
‘method_missing’,
‘undef_method’,
‘class’,
send’,
‘send’].include?(mid)

I think this belongs in the body of the class definition, and not
in the constructor; otherwise, you have to iterate over all the methods
in Proxy_Object every time you create a new Proxy_Object; this will slow
down construction (the same problem that plagues SimpleDelegator).

Also, new() and undef_method() are class methods and not instance
methods, so I don’t think they belong in this list.

If you don’t override inspect(), it might be nice to still be able to
inspect the remote object.

Note that Ruby’s Delegate takes a slightly different approach; instead
of keeping a static list of preserved methods, it assumes that all
methods in Kernel will be preserved except 6 specific methods.

You might also want to look at drb and see how its proxy objects are
implemented.

      self.class.undef_method(mid.intern)

Instead of making undef_method public, you could use:
self.class.instance_eval { undef_method(mid.intern) }

If you move the entire loop outside initialize(), then you can call it
directly:
undef_method(mid.intern)

···

On Wed, Sep 11, 2002 at 07:27:16PM +0900, Tom Sawyer wrote:

    end
  }
end

def method_missing(mid, *args)
  ...act as proxy...
end

end