ptkwt
2/15/2006 7:27:00 AM
In article <200602150449.40928.david@vallner.net>,
David Vallner <david@vallner.net> wrote:
>D=C5=88a Streda 15 Febru=C3=A1r 2006 03:23 Phil Tomson nap=C3=ADsal:
>> I think the answer is that A has already been mixed-in so it's not really
>> mixed-in again (true?).=20
>>
>
>Quite so.
>
>> And how would I go about making this work so that it prints:
>>
>> A::foo
>> B::foo
>> A::foo
>>
>> ?
>>
>
>Make the message printed an instance attribute and change it? *duck*
>
>Mixins aren't plain supposed to do that. They are to add methods, not rewri=
>te=20
>them at whim.
Yeah, I just thought it would be a nice, fairly easy solution.
>If you -have- to do this via metaprogramming, use singleton=20
>methods.
Yeah, I could probably do something like that by redefining the method...
(BTW: Using extend as above makes foo a singleton method )
> Especially in non-trivial code, heavy use of #extend to change=20
>behaviour over and over again would get really messy.
The only way to really get it to work would be to remove_method before doing
the extend. The problem with that is that remove_method doesn't work in that
context (removing singleton methods).
I should probably consider some sort of delegation pattern instead. One
fairly easy way would be to use method_missing and then change the target
object of the message (though I want to avoid overuse of method_missing because
1) it can make things difficult to debug and 2) I might need it for another
purpose in this application. )
Phil