2007/7/23, Jesse Merriman <jesse.d.merriman / gmail.com>:
> Yep, that works just how I want it. I don't particularly like having to
> define #bar everytime it restarts, but it'll do.

Yes, it isn't nice, but given your constraints there's not much you
can do. Maybe instead of defining #bar everytime you'd prefer using a
module:

  class B < SimpleDelegator
    def initialize
      restart_delegate
    end

    def restart_delegate
      delegate = A.new
      delegate.extend WrapperModule
      delegate.init_wrapper self
      __setobj__ delegate
    end

    module WrapperModule
      def init_wrapper wrapper
        @wrapper = wrapper
        @counter = 0
      end

      def bar
        @counter += 1
        @wrapper.restart_delegate if @counter > 2
        "overridden in #{self}, original was #{super}"
      end
    end
  end

Note that there could be a name conflict between the instance
variables of the module and those of the delegate class.

> Thanks, Pit.

You're welcome. Thanks for the puzzle :-)

Regards,
Pit