Wrapping object and method_missing


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

Any help appreciated.



class Wrapper
def method_missing(meth,*args)
return @obj.send(meth,*args)

On Thu, Oct 20, 2011 at 7:04 PM, James F.
[email protected] wrote:

class Wrapper
def method_missing(meth,*args)
return @obj.send(meth,*args)

Posted via http://www.ruby-forum.com/.

Thanks but I wanted to put the code in the wrapped object so it automatically
intercepts method_missing in Wrapper.

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


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs