Forward and Delegate, A Simple Pattern
Here’s a simple example showing how to:
-
Forward all missing method invocations on to some object
-
Dynamically add delegation of that method to that object for future invocation.
def methodmissing(meth, args, &block) #:nodoc: returning @object.send(meth,args, &block) do self.class.classeval %{delegate :#{meth}, :to => :@object} end end
Note this example uses two bits from ActiveSupport:
- returning to avoid explicitly setting a temporary variable for the return value (stylistic choice of mine)
- delegate to generate the delegation code (because it’s dull to do it yourself)
Enjoy!






