Just a note that I recently released a new gem, fast_forward, which adds support for delegation of all missing methods to another object, and does a bit of dynamic method generation to reduce overhead for subsequent calls. The API looks something like:
class Foo
def some_foo_method
"Hello"
end
end
class Bar
fast_forward_to :@foo
def initialize
@foo = Foo.new
end
end
(Note I could have used fast_forward_to :foo if there was an attr_reader for it)
Now, if I create a Bar instance, I can call Foo#some_foo_method transparently using
Bar#some_foo_method:
bar = Bar.new
bar.some_foo_method
=> "Hello"
What’s cool with this setup is the fact that methods are added automatically to the delegating object when they’re invoked for the first time — future invocations don’t need to check up the object hierarchy and resort to method_missing:
bar = Bar.new
bar.respond_to?(:some_foo_method)
=> false
bar.some_foo_method
=> "Hello"
bar.respond_to?(:some_foo_method)
=> true
fast_forward is a small library — but the concepts rolled into it have been pretty handy for me in recent projects.