From: ara.t.howard@... Date: 2006-09-29T03:15:06+09:00 Subject: Re: Multi-Module Alternative to the Factory Pattern? On Fri, 29 Sep 2006, Trans wrote: > Only becuase there are a lot of arguments to pass. hash? array? >> i guess i'm failing to see what multi-module inclusion givs you that >> delegation plus argument passing (into the delegate module not the other >> way around) doesn't? > > The advantage is access to the internal state --no passing of attributes > required. Esspecailly true when you have alot of attributes/arguments > involved. Simple example: > > require 'facet/kernel/as' > > module DEB > A, B, C = %w( D E B ) > def pack_step_one() p A+a end > def pack_step_two() p B+b end > def pack_step_three() p C+c end > end > > module RPM > A, B, C = %w( R P M ) > def pack_step_one() p A+a+b+c end > def pack_step_two() p B+b+c+a end > def pack_step_three() p C+c+a+b end > end > > class Packager > include DEB, RPM > attr :a,:b,:c > def initialize > @a,@b,@c = %w{ 1 2 3 } > end > def pack type > as(type).pack_step_one > as(type).pack_step_two > as(type).pack_step_three > end > end > > packager = Packager.new > packager.pack DEB, 42 > > harp:~ > ruby a.rb > "D123" > "E231" > "B312" > 42 > > Note #as works like #superior from my last example but uses magic-dot > notation to allow any method to be called. (It would be nice if it took a > block but I'll have to work on that). hmm. how bout this: harp:~ > cat a.rb class Object def including m, &b (class << (d=dup); self; end).module_eval{ include m } d.instance_eval &b end end module DEB A, B, C = %w( D E B ) def pack_step_one() p A+a end def pack_step_two() p B+b end def pack_step_three() p C+c end end module RPM A, B, C = %w( R P M ) def pack_step_one() p A+a+b+c end def pack_step_two() p B+b+c+a end def pack_step_three() p C+c+a+b end end class Packager [:a,:b,:c].each{|a| attr a} def initialize @a,@b,@c = %w{ 1 2 3 } end def pack type, *a including(type){ pack_step_one pack_step_two pack_step_three } end end packager = Packager.new packager.pack DEB, 42 harp:~ > ruby a.rb "D1" "E2" "B3" sure the performance will suck though ;-( fun stuff... i like your 'as' method. -a -- in order to be effective truth must penetrate like an arrow - and that is likely to hurt. -- wei wu wei