Suppose I wanted to have an object that uses method_missing which was contained as a private member variable inside a wrapper class. How could I get calls to the wrapper object to route through to the internal object's method_missing, without having to write any code in the wrapper object? I guess I'm after some way of hooking the owning object's method_missing.
class Wrapper
def method_missing(meth,*args)
return @obj.send(meth,*args)
end
end
···
-----Original Message-----
From: Hans Mackowiak [mailto:hanmac@gmx.de]
Sent: 20 October 2011 18:02
To: ruby-talk ML
Subject: Re: wrapping object and method_missing
This cannot work unless the wrapped knows who is wrapping it. And
normally (i.e. when setting a member of any other object to reference
your object) your object wouldn't know when it was wrapped.
Also, it's a questionable design because you introduce a bidirectional
dependency. Also, what do you do in case of multiple wrappers or
changing wrappers?
What is it that you really want to solve?
Kind regards
robert
···
On Thu, Oct 20, 2011 at 7:04 PM, James French <James.French@naturalmotion.com> wrote:
-----Original Message-----
From: Hans Mackowiak [mailto:hanmac@gmx.de]
Sent: 20 October 2011 18:02
To: ruby-talk ML
Subject: Re: wrapping object and method_missing
there:
class Wrapper
def method_missing(meth,*args)
return @obj.send(meth,*args)
end
end
This cannot work unless the wrapped knows who is wrapping it. And
normally (i.e. when setting a member of any other object to reference
your object) your object wouldn't know when it was wrapped.
Also, it's a questionable design because you introduce a bidirectional
dependency. Also, what do you do in case of multiple wrappers or
changing wrappers?
What is it that you really want to solve?
I'm fine with the wrapped object knowing about the wrapper, and agree its necessary. I may not end up using this but I just wanted to know how to do it in ruby. All I'm trying to achieve is a clean interface. If there's no other way I'll just have a wrapper base class that implements method_missing and delegates to internal object.
Cheers for the help.
···
-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]
Sent: 21 October 2011 12:33
To: ruby-talk ML
Subject: Re: wrapping object and method_missing
On Thu, Oct 20, 2011 at 7:04 PM, James French <James.French@naturalmotion.com> wrote:
-----Original Message-----
From: Hans Mackowiak [mailto:hanmac@gmx.de]
Sent: 20 October 2011 18:02
To: ruby-talk ML
Subject: Re: wrapping object and method_missing
there:
class Wrapper
def method_missing(meth,*args)
return @obj.send(meth,*args)
end
end