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