On 6/12/05, Yukihiro Matsumoto <matz / ruby-lang.org> wrote:
> Hi,
> 
> In message "Re: Extending a SimpleDelegator"
>     on Mon, 25 Apr 2005 23:39:35 +0900, TRANS <transfire / gmail.com> writes:
> 
> |I noticed that Delegator (delegate.rb) does not work as one might
> |expect when extending the delegator.
> |  require 'delegate'
> |
> |  M = Module.new {
> |    def bracket ; p "come on" ; end
> |  }
> |
> |  class SD < SimpleDelegator
> |    def initialize( obj )
> |      super( obj )
> |      extend M
> |    end
> |  end
> |
> |  class C
> |    def bracket ;  p "hello" ;  end
> |  end
> |
> |  c = C.new
> |  sd = SD.new(c)
> |  sd.bracket
> |
> |This produces "hello" and not "come on".
> 
> SimpleDelegator creates an object that delegates every method of the
> target object (via singleton methods).  And "extend" adds hidden super
> class between self and its class.  Thus this is expected behavior, IMO.
> 
> I suggest you to use DelegateClass() for the purpose.  This is much
> more efficient.
> 
>   require 'delegate'
> 
>   class C
>     def bracket ;  p "hello" ;  end
>   end
> 
>   M = Module.new {
>     def bracket ; p "come on" ; end
>   }
> 
>   class SD < DelegateClass(C)
>     def initialize(obj)
>       super(obj)
>       extend M
>     end
>   end
> 
>   c = C.new
>   sd = SD.new(c)
>   sd.bracket
> 
>                                                         matz.
> 
> 

Wow! I had all but forgotten about this. Thanks for getting back to
me. I will give this a try.

Thanks,
T.