How about this simple approach:
class Outer def hello puts "hello" end class Inner def initialize outer @outer = outer end def world puts "world" end def method_missing(method, *args, &block) @outer.send(method, *args, &block) rescue NoMethodError
Will be printed
hello cruel is undefined in both inner and outer classes world
In this case, if the inner class does not define the required method, it delegates it to the outer class using Object # send . You can catch a NoMethodError inside method_missing to control when the Outer class does not define a delegated method.
UPDATE You can also use fibers to solve the problem:
class Outer def hello puts "hello" end class Inner def world puts "world" end def method_missing(method, *args, &block) Fiber.yield [method, args, block]
source share