From: Daniel Nugent Date: 2006-02-17T23:10:49+09:00 Subject: Re: extend quesion I see that you've already got a solution, but how about something like this? module A def foo "A::foo" end end module B def foo "B::foo" end end class Demo def to_a @available_modules[:A] = Object.new.extend(A) unless @available_modules[:A] @current_module = @available_modules[:A] end def to_b @available_modules[:B] = Object.new.extend(B) unless @available_modules[:B] @current_module = @available_modules[:B] end #Or more generically def to_module(mod) mod_sym = mod.name.to_sym @available_modules[mod_sym] = Object.new.extend(mod) unless @available_modules[mod_sym] @current_module = @available_modules[mod_sym] end def method_missing(sym, *args) @current_module.send(sym, *args) end def initialize @available_modules = {A.name.to_sym, Object.new.extend(A)} @current_module = [A.name.to_sym] end end On 2/14/06, Phil Tomson wrote: > The following doesn't quite do what I would expect: > > module A > def foo > "A::foo" > end > end > module B > def foo > "B::foo" > end > end > class Demo > def to_a > self.extend(A) > end > def to_b > self.extend(B) > end > def initialize > self.extend(A) > end > end > > > d = Demo.new > puts d.foo #=> A::foo > d.to_b > puts d.foo #=> B::foo > d.to_a > puts d.foo #=> B::foo (but I expected A::foo) > > > OK, I can kind of see why this happended; B's foo 'hide's A's foo method. But > I'm not sure why it didn't happen in the second 'puts' above (ie. why > wouldn't it just keep printing A::foo every time?). I think the answer is that > A has already been mixed-in so it's not really mixed-in again (true?). > > > And how would I go about making this work so that it prints: > > A::foo > B::foo > A::foo > > ? > > Phil > > -- -Dan Nugent