From: Zach Dennis Date: 2005-01-26T04:32:37+09:00 Subject: Re: Self and Ruby Comparisons Trans wrote: > robert, > > Quite right, I'm not sure Ruby is particularly suited for role > swapping. (Prototype-based OOP handles it gracefully) And I don't know > how inefficent #become would be. It would be nice to think its just a > matter of changing one pointer, but I know that's not the case. > > Your role delegator is nice. At the very least it's much easier to be > more than one thing at a time. (There's no problem with roles > interfereing with each at the method name level is there?) Unfortuately > I suspect that it may be nearly as inefficient as repeated using > #become (but that's just a guess). > > Thanks for the illustration, I think I'll store this for potential > future use/exploration. > > BTW, does #unrole actually work? From looking at the Core API it doesn't appear there is an easy way to remove included Modules. I'd suggest to wrap *behaviors* into a Role object, and add that role object to a Person. class Person @roles = [] def add_role( r ); @roles << role end class Role @behaviors = [] def add_behavior( b ); @behaviors << b; end def each_behavior( &b ); @behaviors.each{ |b| yield b.do }; end end class Behavior end class StopFireBehavior < Behavior end class FiremanRole < Role def initialze add_behavior( StopFireBehavior.new ) end end joe = Person.new fireman_role = Role.new( FiremanBehavior.new ) joe.add_role( FiremanRole.new ) This is just thinking outloud, not testing... so a person could have a role, each role is defined by a set of behaviors...? Zach