# Proxying method calls

**URL:** <https://www.ruby-forum.com/t/proxying-method-calls/187277>\
**Category:** Ruby\
**Created:** [April 20, 2010, 11:50pm UTC](https://www.ruby-forum.com/t/proxying-method-calls/187277 "2010-04-20T23:50:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 20, 2010, 11:50pm UTC](https://www.ruby-forum.com/t/proxying-method-calls/187277/1 "2010-04-20T23:50:36Z")

</div>

How do I safely proxy method calls from one class to another, as a  
fallback for when the main class doesn’t respond to the call in  
question? Is my implementation of respond\_to? and method\_missing below  
correct (passing tests at bottom)?

class A  
def initialize  
@b = B.new  
end

```
def respond_to? call
    unless super call
        @b.respond_to? call
    end
end

def method_missing call
    if @b.respond_to? call
        @b.send call
    else
        super
    end
end

```

end

class B  
def b  
‘b’  
end  
end

require ‘test/unit’

class MethodTest \< Test::Unit::TestCase  
def setup  
@a = A.new  
end

```
def test_method_missing
    assert_equal 'b', @a.b
    assert_raise NoMethodError do
        @a.c
    end
end

def test_respond_to
    assert @a.respond_to? :b
    assert_equal false, @a.respond_to?(:c)
end

```

end

Thomas

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/proxying-method-calls/187277/2 "2010-04-21T18:28:12Z")

</div>

On 04/20/2010 11:48 PM, Thomas A. wrote:

> ```
> def respond_to? call
> unless super call
> @b.respond_to? call
> end
> end
> 
> ```

I am wary about respond\_to? - usually I would not bother to override  
this. The reason is that there is no guarantee that an object really  
responds to the method if respond\_to? returns “true” - and the other way  
round.

> ```
> def method_missing call
> if @b.respond_to? call
> @b.send call
> else
> super
> end
> end
> 
> ```

There is at least one issue: you do not propagate method arguments  
properly. Better do

```
 def method_missing(*args, &block)
   @b.send(*args, &block)
 end

```

Delegating to super is only necessary if the super class also implements  
method\_missing. Otherwise you’ll get an exception from @b anyway.

Please see also classes Delegator and SimpleDelegator.  
[http://ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html)

Kind regards

robert
